如何填充本地 Table?

How To Populate A Local Table?

我有一个存储过程,它(尝试)将 table(从查询派生)分配给一个名为 @relation 的变量。这是我目前徒劳的尝试:

ALTER PROCEDURE [dbo].[getAllBookings]
@mode INT
AS
BEGIN
SET NOCOUNT ON;
--==========
DECLARE @relation AS TABLE(
        id INT PRIMARY KEY, code VARCHAR(10), [description] VARCHAR(255),
        environment VARCHAR(10), systems VARCHAR(50), [start] SMALLDATETIME,
        [end] SMALLDATETIME, [user] VARCHAR(50), approved BIT, rejected BIT
    ); 
SELECT * INTO [@relation] FROM 
SELECT 
 B_ID AS 'id',
 B_Change_ID AS 'code',
 B_Description AS 'description',
 E_Name AS 'environment',
 S_System_Name AS 'systems',
 dbo.fromDateTime(B_Start_Date) AS 'start', 
 dbo.fromDateTime(B_End_Date) AS 'end', 
 U_Name AS 'user',
 dbo.isAppr(B_Reviewed, B_Confirmed) AS 'approved',
 dbo.isRej(B_Reviewed, B_Confirmed) AS 'rejected'
FROM ((((dbo.Bookings INNER JOIN dbo.Users ON B_User_ID = U_ID) 
INNER JOIN dbo.Environment ON B_Environment_ID = E_ID) 
INNER JOIN dbo.BookingApps ON B_ID = BA_bookingID)
INNER JOIN dbo.EnvironmentSystems ON BA_bookingSystemID = ES_ID)
INNER JOIN dbo.System_List ON ES_SystemID = S_ID;

--==========
IF (@mode = 0)
    DELETE FROM @relation WHERE (approved = 0);
SELECT * FROM @relation;
END

我做错了什么?

我还没有尝试构建数据和 运行 它,但我必须制作以下 mod 才能编译它。也许这会帮助您入门。

CREATE PROCEDURE [dbo].[getAllBookings] @mode INT
AS
    BEGIN
        SET NOCOUNT ON;

        --==========
        DECLARE @relation AS TABLE
          (
               [id]            INT PRIMARY KEY
               , [code]        VARCHAR(10)
               , [description] VARCHAR(255)
               , [environment] VARCHAR(10)
               , [systems]     VARCHAR(50)
               , [start]       SMALLDATETIME
               , [end]         SMALLDATETIME
               , [user]        VARCHAR(50)
               , [approved]    BIT
               , [rejected]    BIT
          );

        INSERT INTO @relation
                    ([id]
                     , [code]
                     , [description]
                     , [environment]
                     , [systems]
                     , [start]
                     , [end]
                     , [user]
                     , [approved]
                     , [rejected])
            SELECT [B_ID]                                    AS 'id'
                   , [B_Change_ID]                           AS 'code'
                   , [B_Description]                         AS 'description'
                   , [E_Name]                                AS 'environment'
                   , [S_System_Name]                         AS 'systems'
                   , dbo.fromDateTime([B_Start_Date])        AS 'start'
                   , dbo.fromDateTime([B_End_Date])          AS 'end'
                   , [U_Name]                                AS 'user'
                   , dbo.isAppr([B_Reviewed], [B_Confirmed]) AS 'approved'
                   , dbo.isRej([B_Reviewed], [B_Confirmed])  AS 'rejected'
            FROM   dbo.[Bookings]
                   INNER JOIN dbo.[Users]
                           ON [B_User_ID] = [U_ID]
                   INNER JOIN dbo.[Environment]
                           ON [B_Environment_ID] = [E_ID]
                   INNER JOIN dbo.[BookingApps]
                           ON [B_ID] = [BA_bookingID]
                   INNER JOIN dbo.[EnvironmentSystems]
                           ON [BA_bookingSystemID] = [ES_ID]
                   INNER JOIN dbo.[System_List]
                           ON [ES_SystemID] = [S_ID];

        --==========
        IF ( @mode = 0 )
            DELETE FROM @relation
            WHERE  ( [approved] = 0 );

        SELECT *
        FROM   @relation;
    END