SQL PRINT vs SQL EXECsp_ExecuteSQL, performance and table variablesDoes Detach/Attach or Offline/Online Clear the Buffer Cache for a Particular Database?Buffer Size Changing?Safe alternative to exec(sql)SQL Trigger QuestionWhy high compile times for functions with XML on SQL Server 2016?SQL PRINT statement prints nothingAll Queries are slow on Prod than Dev ServerSQL Server Patch Level Is Inaccurate, Causing Upgrade IssuesDBCC ShrinkFile not working
Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?
Why does a 97 / 92 key piano exist by Bösendorfer?
Can I run 125khz RF circuit on a breadboard?
What happens if I try to grapple mirror image?
Make a Bowl of Alphabet Soup
Would a primitive species be able to learn English from reading books alone?
Mimic lecturing on blackboard, facing audience
Proving a complicated language is not a CFL
Do I have to take mana from my deck or hand when tapping a dual land?
PTIJ: does fasting on Ta'anis Esther give us reward as if we celebrated 2 Purims? (similar to Yom Kippur)
"Oh no!" in Latin
Personal or impersonal in a technical resume
What does "tick" mean in this sentence?
The Digit Triangles
What should be the ideal length of sentences in a blog post for ease of reading?
What is the meaning of "You've never met a graph you didn't like?"
SSH to droplet with non root user
Weird lines in Microsoft Word
Parallel Power Supplies with Ideal Diodes
Echo with obfuscation
Giving feedback to someone without sounding prejudiced
Can I say "fingers" when referring to toes?
How to preserve electronics (computers, iPads and phones) for hundreds of years
Can anyone precisely describe what it means (or feels like) to play exactly what your "inner ear" is hearing?
SQL PRINT vs SQL EXEC
sp_ExecuteSQL, performance and table variablesDoes Detach/Attach or Offline/Online Clear the Buffer Cache for a Particular Database?Buffer Size Changing?Safe alternative to exec(sql)SQL Trigger QuestionWhy high compile times for functions with XML on SQL Server 2016?SQL PRINT statement prints nothingAll Queries are slow on Prod than Dev ServerSQL Server Patch Level Is Inaccurate, Causing Upgrade IssuesDBCC ShrinkFile not working
When executing the query below, if I use PRINT
it prints correctly. I can copy and paste the printed code and execute it. However, if I use EXEC I get the following error:
Is there a way of simplifying what I am doing? Why do SQL PRINT
and SQL EXEC
deliver these two very different result sets?
DECLARE @TableName as NVARCHAR(250), @SQL as VARCHAR(MAX);
DECLARE @TableCursor as CURSOR;
SET @TableCursor = CURSOR FOR
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
AND name like 'HISTORY_MasterList_%'
ORDER BY sobjects.name
OPEN @TableCursor;
FETCH NEXT FROM @TableCursor INTO @TableName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL ='select '''+ @TableName +''', 0
Union All
select All ''Server Count'',count(1) from ['+ @TableName +']
Union All
select All ''Server Cores'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
Union All
select ''Production Servers'',count(1) from ['+ @TableName +'] where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Cores'', sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Server Count after filtering out passive/failover servers'', count(1) from
(select distinct m.ServerName
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Production Server Cores after filtering out passive/failover servers'', sum(convert(decimal(18,0),cores)) from(
select distinct m.ServerName, m.Cores
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Non-Prod SQL Instances downgraded to Developer Edition'',count(1) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod SQL Instance Core Count downgraded to Developer Edition'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod VMs moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''VM Moved/right sized from Prod Env to NonProd''
Union All
select ''Non-Prod VMs TO BE moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''Sent for V2V - non prod split''
'
EXEC @SQL
FETCH NEXT FROM @TableCursor INTO @TableName;
END
CLOSE @TableCursor;
DEALLOCATE @TableCursor;
sql-server sql-server-2012 sql-server-2008 sql-server-2008-r2 t-sql
add a comment |
When executing the query below, if I use PRINT
it prints correctly. I can copy and paste the printed code and execute it. However, if I use EXEC I get the following error:
Is there a way of simplifying what I am doing? Why do SQL PRINT
and SQL EXEC
deliver these two very different result sets?
DECLARE @TableName as NVARCHAR(250), @SQL as VARCHAR(MAX);
DECLARE @TableCursor as CURSOR;
SET @TableCursor = CURSOR FOR
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
AND name like 'HISTORY_MasterList_%'
ORDER BY sobjects.name
OPEN @TableCursor;
FETCH NEXT FROM @TableCursor INTO @TableName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL ='select '''+ @TableName +''', 0
Union All
select All ''Server Count'',count(1) from ['+ @TableName +']
Union All
select All ''Server Cores'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
Union All
select ''Production Servers'',count(1) from ['+ @TableName +'] where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Cores'', sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Server Count after filtering out passive/failover servers'', count(1) from
(select distinct m.ServerName
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Production Server Cores after filtering out passive/failover servers'', sum(convert(decimal(18,0),cores)) from(
select distinct m.ServerName, m.Cores
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Non-Prod SQL Instances downgraded to Developer Edition'',count(1) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod SQL Instance Core Count downgraded to Developer Edition'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod VMs moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''VM Moved/right sized from Prod Env to NonProd''
Union All
select ''Non-Prod VMs TO BE moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''Sent for V2V - non prod split''
'
EXEC @SQL
FETCH NEXT FROM @TableCursor INTO @TableName;
END
CLOSE @TableCursor;
DEALLOCATE @TableCursor;
sql-server sql-server-2012 sql-server-2008 sql-server-2008-r2 t-sql
add a comment |
When executing the query below, if I use PRINT
it prints correctly. I can copy and paste the printed code and execute it. However, if I use EXEC I get the following error:
Is there a way of simplifying what I am doing? Why do SQL PRINT
and SQL EXEC
deliver these two very different result sets?
DECLARE @TableName as NVARCHAR(250), @SQL as VARCHAR(MAX);
DECLARE @TableCursor as CURSOR;
SET @TableCursor = CURSOR FOR
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
AND name like 'HISTORY_MasterList_%'
ORDER BY sobjects.name
OPEN @TableCursor;
FETCH NEXT FROM @TableCursor INTO @TableName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL ='select '''+ @TableName +''', 0
Union All
select All ''Server Count'',count(1) from ['+ @TableName +']
Union All
select All ''Server Cores'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
Union All
select ''Production Servers'',count(1) from ['+ @TableName +'] where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Cores'', sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Server Count after filtering out passive/failover servers'', count(1) from
(select distinct m.ServerName
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Production Server Cores after filtering out passive/failover servers'', sum(convert(decimal(18,0),cores)) from(
select distinct m.ServerName, m.Cores
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Non-Prod SQL Instances downgraded to Developer Edition'',count(1) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod SQL Instance Core Count downgraded to Developer Edition'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod VMs moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''VM Moved/right sized from Prod Env to NonProd''
Union All
select ''Non-Prod VMs TO BE moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''Sent for V2V - non prod split''
'
EXEC @SQL
FETCH NEXT FROM @TableCursor INTO @TableName;
END
CLOSE @TableCursor;
DEALLOCATE @TableCursor;
sql-server sql-server-2012 sql-server-2008 sql-server-2008-r2 t-sql
When executing the query below, if I use PRINT
it prints correctly. I can copy and paste the printed code and execute it. However, if I use EXEC I get the following error:
Is there a way of simplifying what I am doing? Why do SQL PRINT
and SQL EXEC
deliver these two very different result sets?
DECLARE @TableName as NVARCHAR(250), @SQL as VARCHAR(MAX);
DECLARE @TableCursor as CURSOR;
SET @TableCursor = CURSOR FOR
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
AND name like 'HISTORY_MasterList_%'
ORDER BY sobjects.name
OPEN @TableCursor;
FETCH NEXT FROM @TableCursor INTO @TableName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL ='select '''+ @TableName +''', 0
Union All
select All ''Server Count'',count(1) from ['+ @TableName +']
Union All
select All ''Server Cores'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
Union All
select ''Production Servers'',count(1) from ['+ @TableName +'] where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Cores'', sum(convert(decimal(18,0),cores)) from ['+ @TableName +']
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
Union All
select ''Production Server Count after filtering out passive/failover servers'', count(1) from
(select distinct m.ServerName
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Production Server Cores after filtering out passive/failover servers'', sum(convert(decimal(18,0),cores)) from(
select distinct m.ServerName, m.Cores
from ['+ @TableName +'] m
inner join [SQLEnv].[dbo].[vwManView] v on m.ServerName = v.ServerName
where Classification in (''Prod'',''Production'',''Prd'',''Unknown'')
and unit <> 0) aa
Union All
select ''Non-Prod SQL Instances downgraded to Developer Edition'',count(1) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod SQL Instance Core Count downgraded to Developer Edition'',sum(convert(decimal(18,0),cores)) from ['+ @TableName +'] where ''InstanceStatus'' like ''Downgrade%''
Union All
select ''Non-Prod VMs moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''VM Moved/right sized from Prod Env to NonProd''
Union All
select ''Non-Prod VMs TO BE moved from Prod Environments'', count(1) from ['+ @TableName +'] where ServerStatus like ''Sent for V2V - non prod split''
'
EXEC @SQL
FETCH NEXT FROM @TableCursor INTO @TableName;
END
CLOSE @TableCursor;
DEALLOCATE @TableCursor;
sql-server sql-server-2012 sql-server-2008 sql-server-2008-r2 t-sql
sql-server sql-server-2012 sql-server-2008 sql-server-2008-r2 t-sql
edited yesterday
Glorfindel
1,0131816
1,0131816
asked yesterday
VorsterVorster
12910
12910
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The error is saying that ...it is not a valid identifier
.
When you pass EXEC @SQL
, it is expecting that @sql
to represent the name of a stored procedure (or scalar function). Hence the error message: ...it is not a valid identifier
.
If you want to execute a text, to pass dynamically , you should encapsulate that text inside ( )
, like EXEC (@SQL)
.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f232492%2fsql-print-vs-sql-exec%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error is saying that ...it is not a valid identifier
.
When you pass EXEC @SQL
, it is expecting that @sql
to represent the name of a stored procedure (or scalar function). Hence the error message: ...it is not a valid identifier
.
If you want to execute a text, to pass dynamically , you should encapsulate that text inside ( )
, like EXEC (@SQL)
.
add a comment |
The error is saying that ...it is not a valid identifier
.
When you pass EXEC @SQL
, it is expecting that @sql
to represent the name of a stored procedure (or scalar function). Hence the error message: ...it is not a valid identifier
.
If you want to execute a text, to pass dynamically , you should encapsulate that text inside ( )
, like EXEC (@SQL)
.
add a comment |
The error is saying that ...it is not a valid identifier
.
When you pass EXEC @SQL
, it is expecting that @sql
to represent the name of a stored procedure (or scalar function). Hence the error message: ...it is not a valid identifier
.
If you want to execute a text, to pass dynamically , you should encapsulate that text inside ( )
, like EXEC (@SQL)
.
The error is saying that ...it is not a valid identifier
.
When you pass EXEC @SQL
, it is expecting that @sql
to represent the name of a stored procedure (or scalar function). Hence the error message: ...it is not a valid identifier
.
If you want to execute a text, to pass dynamically , you should encapsulate that text inside ( )
, like EXEC (@SQL)
.
answered yesterday
Sabin BioSabin Bio
2,1671819
2,1671819
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f232492%2fsql-print-vs-sql-exec%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown