[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL variable declaration

Posted on 2015-01-16
11
Medium Priority
?
207 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
ID: 40553468
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
ID: 40553473
You can use dynamic SQL.

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

Open in new window

0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40553474
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 500 total points
ID: 40553483
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 14

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 500 total points
ID: 40553485
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
 
LVL 2

Expert Comment

by:Pratik Makwana
ID: 40553497
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
ID: 40553520
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
ID: 40553526
It does work!
0
 

Author Comment

by:BananaFury
ID: 40553546
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 500 total points
ID: 40553555
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
ID: 40553815
Thanks very much for your help
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

873 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