SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic SQL Giving 'Name not a valid identifier error'


Dynamic SQL Giving 'Name not a valid identifier error'

Author
Message
dmounday
dmounday
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 128
I have some SQL I'm going to put in a stored procedure which will insert records into a temp table. However, when I run the following, I recieve a message that the Select statement is not a valid identifier. It looks like it is truncating the statement. But, with the PRINT statement, I know that it is populating the variable just fine. Any ideas as to what I need to do? I've copied the SQL and the output I recieve below:

SQL
DECLARE @TreatyNumber As Varchar(20)
SET @TreatyNumber = '200704.2.6'

DECLARE @SQL As Varchar(max)

SET @SQL = 'SELECT vwT.TreatyNumber, topics.PieceTitle AS topic, subsection.PieceTitle AS Subtopic, question.PieceTitle AS SurveyQuestion, (CONVERT(Varchar, topics.PieceNumber) + CONVERT(Varchar, subsection.PieceNumber)) AS PartID, SAP.ColumnName
FROM Treaty.vwTreaty AS vwT INNER JOIN
Survey.vwSurveyInstance AS vwSI ON vwT.TreatySurveyInstanceId = vwSI.SurveyInstanceId INNER JOIN
Survey.vwSurveyTemplate AS vwST ON vwSI.SurveyInstanceSurveyTemplateId = vwST.SurveyTemplateId INNER JOIN
Survey.vwSurveyPiece topics ON vwST.SurveyTemplateId = topics.SurveyPieceSurveyTemplateId AND topics.SurveyPieceTypeIdCodeName = ' + '''' + 'Topic' + '''' + ' INNER JOIN
Survey.vwSurveyPiece AS subsection ON subsection.ParentSurveyPieceId = topics.SurveyPieceId AND subsection.SurveyPieceTypeIdCodeName = ' + '''' + 'Topic Subsection' + '''' + ' INNER JOIN
Survey.vwSurveyPiece AS question ON question.ParentSurveyPieceId = subsection.SurveyPieceId AND question.SurveyPieceTypeIdCodeName = ' + '''' + 'Question' + '''' + ' INNER JOIN
Survey.SurveyAnswerPart SAP ON question.SurveyPieceId = SAP.SurveyPieceId
WHERE vwT.TreatyNumber = ' + '''' + @TreatyNumber + '''' +
' ORDER BY topics.PieceNumber, subsection.PieceNumber, question.PieceNumber'

PRINT @SQL

DECLARE @temp_provisions As Table (TreatyNum Varchar(20), topic Varchar(100), Subtopic Varchar(100), SurveyQuestion Varchar(100), SurveyPart Varchar(5), ColName Varchar(100))

INSERT INTO @temp_provisions
EXEC @SQL

OUTPUT
SELECT vwT.TreatyNumber, topics.PieceTitle AS topic, subsection.PieceTitle AS Subtopic, question.PieceTitle AS SurveyQuestion, (CONVERT(Varchar, topics.PieceNumber) + CONVERT(Varchar, subsection.PieceNumber)) AS PartID, SAP.ColumnName
FROM Treaty.vwTreaty AS vwT INNER JOIN
Survey.vwSurveyInstance AS vwSI ON vwT.TreatySurveyInstanceId = vwSI.SurveyInstanceId INNER JOIN
Survey.vwSurveyTemplate AS vwST ON vwSI.SurveyInstanceSurveyTemplateId = vwST.SurveyTemplateId INNER JOIN
Survey.vwSurveyPiece topics ON vwST.SurveyTemplateId = topics.SurveyPieceSurveyTemplateId AND topics.SurveyPieceTypeIdCodeName = 'Topic' INNER JOIN
Survey.vwSurveyPiece AS subsection ON subsection.ParentSurveyPieceId = topics.SurveyPieceId AND subsection.SurveyPieceTypeIdCodeName = 'Topic Subsection' INNER JOIN
Survey.vwSurveyPiece AS question ON question.ParentSurveyPieceId = subsection.SurveyPieceId AND question.SurveyPieceTypeIdCodeName = 'Question' INNER JOIN
Survey.SurveyAnswerPart SAP ON question.SurveyPieceId = SAP.SurveyPieceId
WHERE vwT.TreatyNumber = '200704.2.6' ORDER BY topics.PieceNumber, subsection.PieceNumber, question.PieceNumber
Msg 203, Level 16, State 2, Line 21
The name 'SELECT vwT.TreatyNumber, topics.PieceTitle AS topic, subsection.PieceTitle AS Subtopic, question.PieceTitle AS SurveyQuestion, (CONVERT(Varchar, topics.PieceNumber) + CONVERT(Varchar, subsection.PieceNumber)) AS PartID, SAP.ColumnName
FROM Treaty.vwTreaty AS vwT INNER JOIN
Survey.vwSurveyInstance AS vwSI ON vwT.TreatySurveyInstanceId = vwSI.SurveyInstanceId INNER JOIN
Survey.vwSurveyTemplate AS vwST ON vwSI.SurveyInstanceSurveyTemplateId = vwST.SurveyTemplateId INNER JOIN
Survey.vwSurveyPiece topics ON vwST.SurveyTemplateId = topics.SurveyPieceSurveyTemplateId AND topics.SurveyPieceTypeIdCodeName = 'Topic' INNER JOIN
Survey.vw' is not a valid identifier.
KenSimmons
KenSimmons
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1596 Visits: 2614
Try using sp_executesql and see if that fixes it.

Here is a sample that should work.

DECLARE @SQL NVARCHAR(MAX)
DECLARE @TBL TABLE (i int)

SET @SQL = 'SELECT TOP 10 message_id FROM sys.messages'

INSERT INTO @TBL
EXEC sp_executesql @SQL

SELECT * FROM @TBL



Ken Simmons
http://twitter.com/KenSimmons
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86192 Visits: 45229
Replace EXEC @SQL with EXEC (@SQL)

Without the brackets, the assumption is that the content of the variable is a stored procedure name

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


KenSimmons
KenSimmons
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1596 Visits: 2614
The good thing about using sp_executesql is that you can pass the parameter value instead of completely building the string on the fly like this...


DECLARE @SQL NVARCHAR(MAX)
DECLARE @TBL TABLE (i int)

SET @SQL = 'SELECT message_id FROM sys.messages WHERE message_id < @ID'

INSERT INTO @TBL
EXEC sp_executesql @SQL, N'@ID INT' , 1000

SELECT * FROM @TBL



Ken Simmons
http://twitter.com/KenSimmons
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86192 Visits: 45229
Indeed. However looking I can't see any reason why this is dynamic SQL at all.

This should work just fine

DECLARE @TreatyNumber As Varchar(20)
SET @TreatyNumber = '200704.2.6'

DECLARE @temp_provisions As Table (
TreatyNum Varchar(20),
topic Varchar(100),
Subtopic Varchar(100),
SurveyQuestion Varchar(100),
SurveyPart Varchar(5),
ColName Varchar(100)
)

INSERT INTO @temp_provisions (TreatyNum, topic, Subtopic, SurveyQuestion, SurveyPart, ColName)
SELECT vwT.TreatyNumber, topics.PieceTitle AS topic, subsection.PieceTitle AS Subtopic, question.PieceTitle AS SurveyQuestion,
(CONVERT(Varchar, topics.PieceNumber) + CONVERT(Varchar, subsection.PieceNumber)) AS PartID,
SAP.ColumnName
FROM Treaty.vwTreaty AS vwT
INNER JOIN Survey.vwSurveyInstance AS vwSI ON vwT.TreatySurveyInstanceId = vwSI.SurveyInstanceId
INNER JOIN Survey.vwSurveyTemplate AS vwST ON vwSI.SurveyInstanceSurveyTemplateId = vwST.SurveyTemplateId
INNER JOIN Survey.vwSurveyPiece topics ON vwST.SurveyTemplateId = topics.SurveyPieceSurveyTemplateId
AND topics.SurveyPieceTypeIdCodeName = 'Topic'
INNER JOIN Survey.vwSurveyPiece AS subsection ON subsection.ParentSurveyPieceId = topics.SurveyPieceId
AND subsection.SurveyPieceTypeIdCodeName = 'Topic Subsection'
INNER JOIN Survey.vwSurveyPiece AS question ON question.ParentSurveyPieceId = subsection.SurveyPieceId
AND question.SurveyPieceTypeIdCodeName = 'Question'
INNER JOIN Survey.SurveyAnswerPart SAP ON question.SurveyPieceId = SAP.SurveyPieceId
WHERE vwT.TreatyNumber = @TreatyNumber



I've removed the order by as, unless there's an identity column on the table, the order of rows inserted is meaningless. Order by should be used on the select that reads from the table variable.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


dmounday
dmounday
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 128
Actually, I pulled only that query out of my larger script. Other parts are using dynamic SQL. This one isn't. So you're right. But, I finally got it to work. I copied and pasted line-by-line into another query window. I had copied the core sql statement from a dataset I had previously built in SSRS. We think some rogue character might have gotten in there.

But, thanks for the posts. I appreciate seeing and learning from what others share.
Terence Keys
Terence Keys
SSC-Enthusiastic
SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)SSC-Enthusiastic (103 reputation)

Group: General Forum Members
Points: 103 Visits: 570
BRACKETS! :-)

Thank you!
softtesting2012
softtesting2012
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 24
Invalid: exec @sqlQuery

Valid: exec (@sqlQuery)
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15988 Visits: 19524
softtesting2012 (6/5/2013)
Invalid: exec @sqlQuery

Valid: exec (@sqlQuery)


Not necessarily so. Read Gail's post above.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86192 Visits: 45229
Please note: 4 year old answered thread.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search