Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Select @var = EXEC(@stmt)
Message
De
08/06/2009 15:52:59
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01404537
Message ID:
01404543
Vues:
28
Thank you Naomi,

I have looked at the sp_executesql in BOL, but I still do not see what I need. Sorry if I am being dense. Given only the Database name, and the table object_id, I need to get the database name. Here is more of what I am trying to do:
	DECLARE	@DBName		varchar(50),
			@TblName	varchar(50),
			@SQLCmd		varchar(150),
			@IndexFragID int,
			@objectID	int	
	DECLARE tblname CURSOR
	FOR SELECT DBName, objectID, IndexFragID
	FROM IndexFragmentation
	OPEN tblname
	FETCH NEXT FROM tblname INTO @DBName, @objectID, @IndexFragID
	WHILE @@fetch_status = 0
	BEGIN

		SET @SQLCmd = 'SELECT TOP 1 [name] FROM ' + QUOTENAME(@DBName) + 
		'.sys.all_objects where object_id = '+ CAST (@objectID AS VARCHAR(15))
		SET @TblName = EXEC (@SQLCmd)
		
		UPDATE IndexFragmentation
			SET TblName = @TblName WHERE IndexFragID = @IndexFragID

		FETCH NEXT FROM tblname INTO @DBName, @objectID	
		DELETE #dataspace
	END
	CLOSE tblname 
	DEALLOCATE tblname
>>Please tell me what is wrong with this. I was sure that I used this syntax before. What would be a better way to do this??
>>
>>
>>		SET @SQLCmd = 'SELECT TOP 1 [name] FROM ' + QUOTENAME(@DBName) + 
>>		'.sys.all_objects where object_id = '+ CAST (@objectID AS VARCHAR(15))
>>		SET @TblName = EXEC (@SQLCmd)
>>
>>
>>Your help is appreciated.
>
>You can not return tablename this way, you need to create it as a parameter.
>
>declare @SQLCmd varchar(max), @ObjectID int, @DBName varchar(max)
>set @DBName = 'tempdb'
>set @ObjectID = 1
>SET @SQLCmd = 'SELECT TOP 1 @TblName = [name] FROM ' + QUOTENAME(@DBName) +
> '.sys.all_objects where object_id = '+ CAST (@objectID AS VARCHAR(15))
> EXEC sp_executesql @SQLCmd, N'@TblName nvarchar(100) OUTPUT',
> @TblName OUTPUT
>
>
>Check sp_executesql in BOL.
>
>Second example here
>
>http://msdn.microsoft.com/en-us/library/ms188001.aspx
Thank You

Rollin Burr

Politicians and diapers have one thing in common. They should both be changed regularly, and for the same reason.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform