如何填充本地表?

时间:2017-12-08 14:51:20

标签: sql-server tsql stored-procedures relation

我有一个存储过程(尝试)将表(从查询派生)分配到名为@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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我还没有尝试构建数据并运行它,但我必须制作以下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