Solved

SQL variable declaration

Posted on 2015-01-16
11
185 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 65

Accepted Solution

by:
Jim Horn earned 125 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 125 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 13

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 125 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 125 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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 …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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