?
Solved

SQL variable declaration

Posted on 2015-01-16
11
Medium Priority
?
196 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
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 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard 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.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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