Friday, March 9, 2012

Exact dynamic sql problem

The code that i want to fix and where im stuck at
Declare @.server varchar(100)
DECLARE Server_Cursor CURSOR FOR
SELECT srvname from master..sysservers
OPEN Server_Cursor
FETCH NEXT FROM Server_Cursor into @.server
WHILE @.@.FETCH_STATUS = 0
BEGIN
DECLARE Employee_Cursor CURSOR FOR
SELECT LastName, FirstName
FROM @.server.Northwind.dbo.Employees -- This is where the problem is with
the @.Server
WHERE LastName like 'B%'
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor
WHILE @.@.FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Employee_Cursor
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
FETCH NEXT FROM Server_Cursor into @.server
END
CLOSE Server_Cursor
DEALLOCATE Server_Cursor
This is the part within the second nested cursor where the problem is.. Can
someone help me ? Thanks
DECLARE Employee_Cursor CURSOR FOR
SELECT LastName, FirstName
FROM @.server.Northwind.dbo.Employees -- This is where the problem is with
the @.Server
WHERE LastName like 'B%'Rather than show us a broken piece of code could you A) explain what it
is you are trying to achieve B) post enough information to reproduce
the problem. This should help:
http://www.aspfaq.com/etiquette.asp?id=5006
The best advice I can give based on what you posted is to avoid using
cursors and dynamic SQL. More on Dynamic SQL here:
http://www.sommarskog.se/dynamic_sql.html
David Portas
SQL Server MVP
--|||You don't need to use a cursor when using static sets of data. Use set
operations
SELECT srvname
into #temp
from master..sysservers
declare @.server varchar(50)
while exists (select 'x' from #temp)
begin
select top 1 @.server = srvname
from #temp
exec ('SELECT LastName, FirstName
FROM ' + @.server + '.Northwind.dbo.Employees WHERE LastName like
''B%''')
delete from #temp
where srvname = @.server
end
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:eUjU1m7DFHA.2572@.tk2msftngp13.phx.gbl...
> The code that i want to fix and where im stuck at
> Declare @.server varchar(100)
> DECLARE Server_Cursor CURSOR FOR
> SELECT srvname from master..sysservers
> OPEN Server_Cursor
> FETCH NEXT FROM Server_Cursor into @.server
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> DECLARE Employee_Cursor CURSOR FOR
> SELECT LastName, FirstName
> FROM @.server.Northwind.dbo.Employees -- This is where the problem is with
> the @.Server
> WHERE LastName like 'B%'
> OPEN Employee_Cursor
> FETCH NEXT FROM Employee_Cursor
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> FETCH NEXT FROM Employee_Cursor
> END
> CLOSE Employee_Cursor
> DEALLOCATE Employee_Cursor
> FETCH NEXT FROM Server_Cursor into @.server
> END
> CLOSE Server_Cursor
> DEALLOCATE Server_Cursor
> --
> This is the part within the second nested cursor where the problem is..
Can
> someone help me ? Thanks
> DECLARE Employee_Cursor CURSOR FOR
> SELECT LastName, FirstName
> FROM @.server.Northwind.dbo.Employees -- This is where the problem is with
> the @.Server
> WHERE LastName like 'B%'
>

No comments:

Post a Comment