1

I want to pass variables as columns in a Table Variable.

DECLARE @VAR1 NVARCHAR(MAX)
DECLARE @VAR2 NVARCHAR(MAX)

and I have set values in these variables. Now I want to form a table from these results.

This is what I have tried

DECLARE @Query TABLE(@Pvar1,@Total)

Desired output:

Var1   Var2

 abc    xyz
1
  • Do you want to use the variables to determine the field type or content? Commented Jul 31, 2014 at 6:46

1 Answer 1

1

In SQL-Server you cant declare a table variable with other variables, you have to use the same datatype

DECLARE @var1 NVARCHAR(MAX) = 'abc'
DECLARE @var2 NVARCHAR(MAX) = 'xyz'

DECLARE @table TABLE(
   var1 NVARCHAR(MAX),
   var2 NVARCHAR(MAX)
)

INSERT INTO @table SELECT @var1, @var2

SELECT * FROM @table
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.