| Author |
Topic |
|
HellBoy
Starting Member
9 Posts |
Posted - 2007-12-27 : 10:08:24
|
| Declare @DBName sysnameSet @DBName = 'Pubs'EXEC ('USE '+@DBName+';DECLARE Header CURSOR FOR SELECT * FROM dbo.titles')I want to use the DBname variable with cursor but it wasn't workSkyDiver |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-27 : 10:12:50
|
quote: Originally posted by HellBoy Declare @DBName sysnameSet @DBName = 'Pubs'EXEC ('USE '+@DBName+';DECLARE Header CURSOR FOR SELECT * FROM '+@DBName+'.dbo.titles')I want to use the DBname variable with cursor but it wasn't workSkyDiver
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
HellBoy
Starting Member
9 Posts |
Posted - 2007-12-27 : 10:22:49
|
| Declare @DBName sysnameSet @DBName = 'Pubs'DECLARE Header CURSOR FOR SELECT * FROM '+@DBName+'.dbo.titles')I tried to use this but not work - I used SQL Server 2000SkyDiver |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-27 : 10:24:10
|
| I don't think that will work either. Your cursor will only be "in scope" for the exec'd process. any variable declared in dynamic sql (including a cursor) will only be available within that process.Be One with the OptimizerTG |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-12-27 : 10:24:56
|
| tell us why you want to do this and we'll tell you of a better way if there is one._______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out |
 |
|
|
HellBoy
Starting Member
9 Posts |
Posted - 2007-12-27 : 10:29:04
|
| I made a Procedures to insert data from table to another and i used a cursor function and i want to use the database name as dynamic name.SkyDiver |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-27 : 10:39:08
|
| TG,I think the scope of cursor is Global by default which means it can be accessed outside EXEC.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-27 : 10:58:27
|
harsh, you're right. I just tested it. I never tried to do that before and just assumed you couldn't.declare @title_id varchar(25)exec('declare crs cursor for select title_id from pubs..titles')open crsfetch next from crs into @title_idwhile @@fetch_status = 0begin print '@title_id: ' + @title_id fetch next from crs into @title_idendclose crsdeallocate crsBe One with the OptimizerTG |
 |
|
|
HellBoy
Starting Member
9 Posts |
Posted - 2007-12-27 : 11:00:10
|
| Declare @DBName sysnameSet @DBName = 'Pubs'DECLARE Header CURSOR FOR SELECT * FROM [DBname].dbo.titlesWhat can i do to use dynamic Database name in this caseSkyDiver |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-12-27 : 11:04:09
|
to expand TG's code:Declare @DBName sysnameSet @DBName = '[Pubs]'declare @title_id varchar(25)exec('declare crs cursor for select title_id from ' + @DBName + '.dbo.titles')open crsfetch next from crs into @title_idwhile @@fetch_status = 0begin print '@title_id: ' + @title_id fetch next from crs into @title_idendclose crsdeallocate crs_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-27 : 11:05:44
|
| That's what I initially suggested, not sure why it didn't work for HellBoy though.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
HellBoy
Starting Member
9 Posts |
Posted - 2007-12-27 : 11:41:11
|
| Thanks for all,it works now harsh.SkyDiver |
 |
|
|
|