dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: SQL Server
Expertise: Beginner
May 2, 2000

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Problem with Select Statement

Question:
I'm writing a select statement to make a table name a variable whose value is set according to a condition:
Var tablename;
tablename := emp table
select * from tablename;
But I get an error message. How do I make this statement work?

Answer:
Your syntax is close to what you need. But you need to call the EXECUTE command with the SQL string. Here's code you can run in Query Analyzer that uses a variable for the table name and retrieves all the records from the Employees table:

DECLARE @TableName varchar(255)
SET @TableName = 'Employees'
EXEC ('SELECT * FROM ' + @TableName)
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date