SQL Server SELECT INTO @variable?

前端 未结 7 1447
走了就别回头了
走了就别回头了 2020-12-02 03:56

I have the following code in one of my Sql (2008) Stored Procs which executes perfectly fine:

    CREATE PROCEDURE [dbo].[Item_AddItem]
        @CustomerId u         


        
相关标签:
7条回答
  • 2020-12-02 04:26

    I found your question looking for a solution to the same problem; and what other answers fail to point is a way to use a variable to change the name of the table for every execution of your procedure in a permanent form, not temporary.

    So far what I do is concatenate the entire SQL code with the variables to use. Like this:

    declare @table_name as varchar(30)
    select @table_name = CONVERT(varchar(30), getdate(), 112)
    set @table_name = 'DAILY_SNAPSHOT_' + @table_name
    
    EXEC('
            SELECT var1, var2, var3
            INTO '+@table_name+'
            FROM my_view
            WHERE string = ''Strings must use double apostrophe''
        ');
    

    I hope it helps, but it could be cumbersome if the code is too large, so if you've found a better way, please share!

    0 讨论(0)
  • 2020-12-02 04:27

    If you wanted to simply assign some variables for later use, you can do them in one shot with something along these lines:

    declare @var1 int,@var2 int,@var3 int;
    
    select 
        @var1 = field1,
        @var2 = field2,
        @var3 = field3
    from
        table
    where
        condition
    

    If that's the type of thing you're after

    0 讨论(0)
  • 2020-12-02 04:32

    It looks like your syntax is slightly out. This has some good examples

    DECLARE @TempCustomer TABLE
    (
       CustomerId uniqueidentifier,
       FirstName nvarchar(100),
       LastName nvarchar(100),
       Email nvarchar(100)
    );
    INSERT @TempCustomer 
    SELECT 
        CustomerId, 
        FirstName, 
        LastName, 
        Email 
    FROM 
        Customer
    WHERE 
        CustomerId = @CustomerId
    

    Then later

    SELECT CustomerId FROM @TempCustomer
    
    0 讨论(0)
  • 2020-12-02 04:43

    Sounds like you want temp tables. http://www.sqlteam.com/article/temporary-tables

    Note that #TempTable is available throughout your SP.

    Note the ##TempTable is available to all.

    0 讨论(0)
  • 2020-12-02 04:46
    "SELECT *
      INTO 
        @TempCustomer 
    FROM 
        Customer
    WHERE 
        CustomerId = @CustomerId"
    

    Which means creating a new @tempCustomer tablevariable and inserting data FROM Customer. You had already declared it above so no need of again declaring. Better to go with

    INSERT INTO @tempCustomer SELECT * FROM Customer
    
    0 讨论(0)
  • 2020-12-02 04:47

    You cannot SELECT .. INTO .. a TABLE VARIABLE. The best you can do is create it first, then insert into it. Your 2nd snippet has to be

    DECLARE @TempCustomer TABLE
    (
       CustomerId uniqueidentifier,
       FirstName nvarchar(100),
       LastName nvarchar(100),
       Email nvarchar(100)
    );
    INSERT INTO 
        @TempCustomer 
    SELECT 
        CustomerId, 
        FirstName, 
        LastName, 
        Email 
    FROM 
        Customer
    WHERE 
        CustomerId = @CustomerId
    
    0 讨论(0)
提交回复
热议问题