Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic SQL 1

Status
Not open for further replies.

drallabs

Technical User
Mar 2, 2006
28
US
The following dynamic sql statement executes the first print statement when the @standid variable is not null. Any ideas, perhaps a dynamic sql qwerk?

declare @standid varchar(100)
Declare @SQL VarChar(1000)

SELECT @SQL = ' SELECT' + @standid + '= standid FROM ' + @TableName + ' where standid is null'
If @standid is null
Begin
print 'ERROR. There are missing standids in the standedits Layer. '
print''
End
Else
print 'NO ERRORS. There arent any Missing Standids.'
print''
 
You can't set a variable like this using dynamic SQL.

You need to create a temp table in the calling code, and have the dynamic SQL insert into the temp table. Then get the value from the temp table and do your logic.

Denny
MCSA (2003) / MCDBA (SQL 2000) / MCTS (SQL 2005) / MCITP Database Administrator (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Thanks for your response. I dont understand exactly what you mean, an example would help. I should also mention that the statement exists within a SP.

CREATE Procedure StandEdit_ErrorCheck
@TableName VarChar(100)
AS
--check for missing standid values
declare @standid varchar(100)
Declare @SQL VarChar(1000)

SELECT @SQL = ' SELECT' + @standid + '= standid FROM ' + @TableName + ' where standid is null'
If @standid is null
Begin
print 'ERROR. There are missing standids in the standedits Layer. '
print''
End
Else
print 'NO ERRORS. There arent any Missing Standids.'
print''
Exec( @SQL )
GO

On another note can you recommend a book that covers Dynamic SQL? The on-line help doesnt cut it.
 
you can use sp_executesql with output parameters or exec with a temp table

here is an example of both
Code:
USE pubs
GO

--sp_executesql
DECLARE @chvTableName VARCHAR(100),
@intTableCount INT,
@chvSQL NVARCHAR(100)

SELECT @chvTableName = 'Authors'
SELECT @chvSQL = N'SELECT @intTableCount = COUNT(*) FROM ' + @chvTableName

EXEC sp_executesql @chvSQL, N'@intTableCount INT OUTPUT', @intTableCount OUTPUT

SELECT @intTableCount
GO


--EXEC (SQL)
DECLARE @chvTableName VARCHAR(100),
@intTableCount INT,
@chvSQL NVARCHAR(100)


CREATE TABLE #temp (Totalcount INT)
SELECT @chvTableName = 'Authors'
SELECT @chvSQL = 'Insert into #temp Select Count(*) from ' + @chvTableName

EXEC( @chvSQL)

SELECT @intTableCount = Totalcount from #temp

SELECT @intTableCount

DROP TABLE #temp

a really good page about dynamic SQl is here


Denis The SQL Menace
SQL blog:
Personal Blog:
 
Thanks for your response SQLDenis, I dont entirely understand all the syntax, specifically what is the purpose of N'?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top