General information
Forum:
Microsoft SQL Server
Hi Sergey,
Thanks. This will do the trick. I guess this is the only way then to perform the equivalent of macro substitution.
Also, Bob Archer sent me a very helpful reply regarding an article on the sp_MSForEach SP's, which I didn't know about. It looks like they contain the majority of the code that I was intending to write from scratch, and they use EXECUTE in the manner you suggested.
>Hi David,
>
>Take a look at EXECUTE T-SQl command and sp_executesql system stored procedure in BOL.
>
>>I am attempting to write a tsql script that will replicate a given operation across all databases on a given sql server (they all have the same tables, sp's etc). So far, I have written the code below. But I don't know of a way to reference a variable name in a tsql statement, or perform the equivalent of macro substition in vfp
>>For example, for each database in the schema, I want to be able to 'use it', and then do some further operation on it, like create a stored procedure. But the code doesnt work. In the code below the USE statement (which is commented out) doesn't work, nor does the statement:
>>select * from @ccatlogname.archived
>>
>>Is this possible in tsql?
>>
>>TIA
>>
>>
>>
>>
>>declare @cCatalogName char(100)
>>
>>declare dblist scroll cursor
>>for
>>select catalog_name from master.information_schema.schemata
>>
>>OPEN dblist
>>
>>
>>
>>FETCH NEXT FROM dblist
>>INTO @cCatalogName
>>print @ccatalogname
>>WHILE @@FETCH_STATUS = 0
>>BEGIN
>> FETCH NEXT FROM dblist
>> INTO @cCatalogName
>> SET @ccatalogname=rtrim(@ccatalogname)
>> print @ccatalogname
>>
>> /*use @ccatalogname */
>> select * from @ccatlogname.archived
>>END
>>
>>CLOSE dblist
>>deallocate dblist
Previous
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only