Solved

SQL variable declaration

Posted on 2015-01-16
11
171 Views
Last Modified: 2015-01-16
Hello

Nice easy one I am sure

This works
declare @Prefix varchar(60);
declare @Date varchar(60);
set @Prefix = 'mi.Archive_tblFinanceSummary_';
set @Date = '2015_01_10';
select @Prefix + @Date

but I am unable to use the same variable as part of the FROM clause, as such

declare @Prefix varchar(60);
declare @Date varchar(60);
set @Prefix = 'mi.Archive_tblFinanceSummary_';
set @Date = '2015_01_10';
select * from @Prefix + @Date

Says incorrect syntax. Why?

Thanks
0
Comment
Question by:BananaFury
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Because "@Prefix + @Date" is not a table; it's the results of a function.

Only things which result in a table can be in the FROM.
0
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
You can use dynamic SQL.

exec 'select * from ' + @Prefix + @Date

Open in new window

0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 125 total points
Comment Utility
Need nvarchar's and dynamic SQL to pull that off, as you can't substitute variables for object names.
declare @Prefix varchar(60);
declare @Date varchar(60);
set @Prefix = 'mi.Archive_tblFinanceSummary_';
set @Date = '2015_01_10';

Declare @sql nvarchar(100) 
SET @sql = 'select * from ' + @Prefix + @Date

exec sp_executesql @sql

Open in new window

0
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 125 total points
Comment Utility
Better yet, use sp_executesql

declare @sql = nvarchar(100)
select @sql = 'select * from ' + @Prefix + @Date
exec sp_executesql @sql

Open in new window

0
 
LVL 11

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 125 total points
Comment Utility
You can use the following to achieve usage of dynamic table names in the same query:

DECLARE @Prefix VARCHAR(60) ;
DECLARE @Date VARCHAR(60) ;
SET @Prefix = 'mi.Archive_tblFinanceSummary_' ;
SET @Date = '2015_01_10' ;
 --select * from @Prefix + @Date
 
DECLARE @sqlStmnt VARCHAR(MAX) = '';

SELECT @sqlStmnt = 'SELECT * FROM ' + @Prefix + @Date;

SELECT @sqlStmnt; --Returns: SELECT * FROM mi.Archive_tblFinanceSummary_2015_01_10

--To execute the dynamically built SQL
EXEC @sqlStmnt;

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:Pratik Makwana
Comment Utility
Use this....
declare @Prefix nvarchar(60);
declare @Date nvarchar(60);
declare @command nvarchar(100);
set @Prefix = ''mi.Archive_tblFinanceSummary_'';
set @Date = '2015_01_10';
set @command = 'select * from ' + @Prefix + @Date

exec sp_executesql @comma

Open in new window

0
 

Author Comment

by:BananaFury
Comment Utility
So easy, I knew it wouldn't be a test

quick question though, so I understand.

why does this work

declare @Prefix varchar(60);
declare @Date varchar(60);
set @Prefix = 'mi.Archive_tblFinanceSummary_';
set @Date = '2015_01_12';
exec ('select * from ' + @Prefix + @Date)

When this doesn't

declare @Prefix varchar(60);
 declare @Date varchar(60);
 set @Prefix = 'mi.Archive_tblFinanceSummary_';
 set @Date = '2015_01_10';
 select @Prefix + @Date

I would like to understand why it needs executing separately so I might learn a lesson

Thanks so much
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
It does work!
0
 

Author Comment

by:BananaFury
Comment Utility
sorry I meant when this doesn't

declare @Prefix varchar(60);
 declare @Date varchar(60);
 set @Prefix = 'mi.Archive_tblFinanceSummary_';
 set @Date = '2015_01_10';
 select * from @Prefix + @Date

but this does

declare @Prefix varchar(60);
declare @Date varchar(60);
set @Prefix = 'mi.Archive_tblFinanceSummary_';
set @Date = '2015_01_12';

exec ('select * from ' + @Prefix + @Date)
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 125 total points
Comment Utility
It's because you have a table called "mi.Archive_tblFinanceSummary_2015_01_12".

You are creating the entire command as a string in the second version, whereas in the first version it expects @Prefix to be a table.
0
 

Author Closing Comment

by:BananaFury
Comment Utility
Thanks very much for your help
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now