Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am still a beginner, but I am having trouble with a T-SQL scalar variable during a create table. Can anyone tell me what is wrong with this script?

-- T-SQL MSSQL
SET NOCOUNT ON
DECLARE @tt = 'TempNWA'
--Create Temporary Table
CREATE TABLE [@tt] 
 (  [Table_Name] varchar(50),
    Row_Count int,
    Table_Size varchar(50),
    Data_Space_Used varchar(50),
    Index_Space_Used varchar(50),
    Unused_Space varchar(50)
 )
DROP TABLE [@tt]

The error I get, which I can't decipher is:

Incorrect syntax near '='
share|improve this question
add comment

closed as off topic by Martin Smith, RolandoMySQLDBA, Mark Storey-Smith, dezso, Paul White Jan 20 '13 at 10:07

Questions on Database Administrators Stack Exchange are expected to relate to database administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 3 down vote accepted
DECLARE @tt = 'TempNWA'

is missing any dataype declaration. You haven't explained what you are trying to do exactly but I assume you need something like.

SET NOCOUNT ON
DECLARE @TempTable SYSNAME = 'TempNWA'   


DECLARE @SQL NVARCHAR(MAX) = N'
CREATE TABLE ' + QUOTENAME(@TempTable) + N'  
 (  [Table_Name] varchar(50),
    Row_Count int,
    Table_Size varchar(50),
    Data_Space_Used varchar(50),
    Index_Space_Used varchar(50),
    Unused_Space varchar(50)
 );

DROP TABLE ' + QUOTENAME(@TempTable) + N';'

EXEC(@SQL)

You cannot parameterise object names so would need to use dynamic SQL for this See Dealing with Dynamic Table and Column Names for more.

share|improve this answer
    
That seems to work. Thanks very much. I Google'd it but didn't see an answer as good as yours. –  djangofan Jan 18 '13 at 19:52
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.