[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL variable declaration

Posted on 2015-01-16
11
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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