Helpful Information
 
 
Category: MS SQL Development
Problem with sp_executesql

My query runs clean however, it does absolutely nothing when using sp_executesql. When I run it without using sp_executesql it works fine, but I need sp_executesql to pass in different db names. When I run the query, no rows are updated and when I print my @SQLString variable it doesn't return anything at all. In looking through the server trace in QA everything is being set and executed like it should be... Any ideas? (Sorry for the voluminous code)

DECLARE @Id int, @Name varchar(100)
Set @id = 5
Set @Name = 'Shire'

Declare @SQLString nvarchar (4000)

DECLARE @IDAsChar nvarchar(100)

SET @SQLString = N'create table #objects (objectID int, dbID smallint)
insert into #objects (ObjectID, dbID)
select distinct o.id, ucv.dbID
from [[[Benditt.][' + @Name + '].][dbo].]sysobjects o join [[[Benditt.][' + @Name + '].][dbo].]syscomments c (nolock) on o.id = c.id
left outer join UWCustom.dbo.UW_Code_Versions ucv (nolock)
on o.id = ucv.objectId
and o.xtype = ucv.xtype
and u.name = ucv.OwnerName
and "' + @Name + '" = ucv.dbName
and o.name = ucv.ObjectName
and c.colid = ucv.colid
and ' + @IDAsChar + '= ucv.dbID
and LastVersion = "Y"
where o.xtype in ("P", "V")
and checksum(c.text) <> ucv.TextCheckSum


update UWCustom.dbo.uw_code_versions
set LastVersion = "N"
where ObjectID IN (select ObjectID from #Objects)
and dbID = ' + @IDAsChar + '

Insert into UWCustom.dbo.UW_Code_versions (
ObjectID
, xtype
, dbName
, dbID
, Ownername
, ObjectName
, colid
, DateEntered
, TextCheckSum
, text
)
select
o.id
, o.xtype
, ' + @Name + ' , ' + @IDAsChar + ' , u.name
, o.name
, c.colid
, getdate()
, checksum(c.text)
, c.text
from
#objects
join [[[Benditt.][' + @Name + '].][dbo].]sysobjects o (nolock) on #Objects.ObjectID = o.id
join [[[Benditt.][' + @Name + '].][dbo].]syscomments c (nolock) on o.id = c.id
join [[[Benditt.][' + @Name + '].][dbo].]sysusers u (nolock) on o.uid = u.uid

drop table #Objects'

PRINT @SQLString

EXEC sp_executesql @SQLString

GO

ciao,
ho avuto il tuo stesso problema... e sono capitata sul tuo post.
io ho risolto usando le tabelle temporanee con i doppi cancelletti (##tmpTable)
spero ti possa essere di aiuto
k@y

Hi,

Your problem is that you are declaring :

DECLARE @IDAsChar nvarchar(100)

but then not setting anything to it, it is defaulting to NULL which then means that when you try to join a 'NULL' to any other text :

and ' + @IDAsChar + '= ucv.dbID

You'll get a NULL returned when you try to print it.

Hope this helps,

Alaistair










privacy (GDPR)