t-sql dynamic sql execute twice with different column names

I have a dynamic sql statement that I need to execute twice with different column names.  How can I do this:
declare @cols as nvarchar(50)
declare @col_name as nvarchar(50)
declare @tables as nvarchar(50)
declare @sql as nvarchar(300)

set @cols = 'Name'--original column name
set @tables = 'Bank'
set @col_name = 'NAME123'--  display column name

set @sql = N'SELECT '+ @cols +' as '+@col_name+'  FROM ' + @tables 
EXECUTE sp_executesql @sql

set @col_name = 'NAME456'--  display column name
EXECUTE sp_executesql @sql

Open in new window

maverick0728Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You'll need to set again the @sql string for the second call:
--  2nd call
set @col_name = 'NAME456'
set @sql = N'SELECT '+ @cols +' as '+@col_name+'  FROM ' + @tables 
EXECUTE sp_executesql @sql

Open in new window

0
Nitin SontakkeDeveloperCommented:
Preferably encapsulating within square brackets while constructing a string itself.
0
Mark WillsTopic AdvisorCommented:
Well, unless you want to capture results in between executes, you could do
declare @cols as nvarchar(50)
declare @col_name as nvarchar(50)
declare @tables as nvarchar(50)
declare @sql as nvarchar(300)

set @cols = 'Name'--original column name
set @tables = 'Bank'
set @col_name = 'NAME123'--  display column name

set @sql = N'SELECT '+ @cols +' as '+@col_name+'  FROM ' + @tables 

set @col_name = 'NAME456'--  display column name

set @sql = @sql + N'
SELECT '+ @cols +' as '+@col_name+'  FROM ' + @tables 

EXECUTE sp_executesql @sql

Open in new window

0
maverick0728Author Commented:
I only want to declare the @sql once and feed it the first column name, execute, then feed it the second column name and execute it again.  Maybe dynamic sql isn't the way to go here.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
But it's declared only once. Only thing is that you'll need to set the variable value every time you'll need to run a different SELECT.

Other option is using a stored procedure:
CREATE PROCEDURE MySQLExecute (@cols nvarchar(50), @col_name nvarchar(50), @tables nvarchar(50))
AS
	declare @sql as nvarchar(300)

	set @sql = N'SELECT '+ @cols +' as '+@col_name+'  FROM ' + @tables 
	EXECUTE sp_executesql @sql
GO

Open in new window

After you creating the SP, by running the above code, you just need to call it like the following:
declare @cols as nvarchar(50)
declare @col_name as nvarchar(50)
declare @tables as nvarchar(50)

set @cols = 'Name'--original column name
set @tables = 'Bank'
set @col_name = 'NAME123'--  display column name
EXEC MySQLExecute @cols, @col_name, @tables

--2nd call
set @col_name = 'NAME456'--  display column name
EXEC MySQLExecute @cols, @col_name, @tables

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.