| Author |
Topic |
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-08 : 10:17:29
|
| I am having a problem with the following portion of my statement.I am getting:Incorrect syntax near the keyword 'exec'.When I change the exec to select it just returns a string and doesnt execute the statement. Any help?declare crs_classname cursor for exec ('select name from ' + @JerryReturn + ' where name is not null') |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-08 : 10:47:34
|
| You can't define a cursor as an EXEC'd dynamic sql statement.Ok, I'm torn here. I tend to want to help people but in this case I feel like HalaszJ has a gun pointed to his head, his finger near but not on the trigger. He asks, "please help me put my finger on the trigger so I can pull it."In case it wasn't obvious HalaszJ, the gun represents the cursor. Are you sure you need to use a cursor? Are you sure you need to use dynamic sql to define your cursor? What are you trying to do (big picture)?Be One with the OptimizerTG |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-08 : 10:59:12
|
| no gun here, just trying to get a bunch of data out of a table.what i have is a table that has all the info i need except 1 thing, that 1 thing is in another table that is a field in the first table, i need to go into that table to get the last item.Hmm, that sounds hard to understand let me try to give an example.TableAField1 Field2 Field3Field3 has the name of the other table i need to select from.Table? (Field3 result)Field1I do not know of any other way to do this since Field3 (table) does not have any keys or anything i can join off of in order to to a proper statement.Does this make any sense? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-08 : 12:00:17
|
Ok, I see the problem now. Yes, that design is pretty nasty. When you have a design like this the sql becomes very ugly. Anyway, if you're stuck with it maybe something like this would be better:use northwindset nocount on-----------------------------------------------------DDL/DMLcreate table myTable (myTableColumn varchar(15))create table table1 (name varchar(15))create table table2 (name varchar(15))goinsert myTable select 'Table1' union select 'table2'insert table1 (name)select 'tg' union allselect nullinsert table1 (name)select 'HalaszJ' union allselect nullgo-----------------------------------------------------Get all names from all tablesdeclare @sql varchar(8000)select @sql = coalesce (@sql + ' Union all ' + 'select name from ' + myTableColumn + ' where name is not null', 'select name from ' + myTableColumn + ' where name is not null')from myTablewhere myTablecolumn is NOT NULL--print @sqlexec(@sql)godrop table myTabledrop table table1drop table table2 Be One with the OptimizerTG |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-08 : 12:18:57
|
| Wow, that is way over my head, i do not know what to even change in order to make that work, what i have now is the following:any help would be much appriciatedselect p.manufacturer, p.name, p.version, c.memberclassname, ' ' 'JerryReturn'into #JerryReturnDatafrom v_package p join v_advertisement a on p.packageid = a.packageid join v_collection c on a.collectionid = c.collectionidwhere a.advertisementname like 'web%' declare @name varchar(1000), @classnamelist varchar(1000)declare @Jerryreturn varchar(1000)declare crs_JerryReturn cursor for select distinct memberclassname from #JerryReturnData open crs_JerryReturn fetch next from crs_JerryReturn into @JerryReturndeclare @printcrap varchar(100) while @@fetch_status = 0 begin set @printcrap = 'select name into ##temp from ' + @JerryReturn + ' where name is not null' exec (@printcrap) declare crs_classname cursor for select name from ##temp open crs_classname fetch next from crs_classname into @name while @@fetch_status = 0 begin select @classnamelist = @classnamelist + @name + ',' fetch next from crs_classname into @name end close crs_classname deallocate crs_classname drop table ##temp update #JerryReturnData set JerryReturn = ISNULL(@classnamelist, '') where memberclassname = @JerryReturn fetch next from crs_JerryReturn into @JerryReturn endclose crs_JerryReturndeallocate crs_JerryReturn |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-08 : 14:20:37
|
| My solution could work well for this depending on...How many rows can be in #JerryReturnData? Less than 150?Be One with the OptimizerTG |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-08 : 14:26:35
|
| there are a few thousand rows in the #JerryReturnData table. Now the other table that needs to be queried will have a return of less than 50, and i was stringing them together and throw them into the 'JerryReturn' field. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-08 : 14:48:40
|
| >>select distinct memberclassname from #JerryReturnDataSo this returns a few thousand rows? or less than 50?Be One with the OptimizerTG |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-08 : 14:55:36
|
| few thousand, well... i think its actually 1500 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-11-08 : 14:56:00
|
| First, TG's code can be copied and paste into a Query Analyzer window, and it will just run (hold, that, let me check first...)...OK, it doesPlease read the hint link in my sig, post some of the requested info, and we'll get you an answer asapRight now it's just a guessing gameBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-08 : 17:14:50
|
| thanks, ill take a look when i get home, for some reason that link is blocked by websense here at work. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-08 : 20:04:30
|
| So you've got 1500 different tables out there, all with a [name] column and you need data from each one of them for this? Why isn't all in this data in the same table?Be One with the OptimizerTG |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-09 : 09:47:15
|
| because MS didnt design it that way, this is a query for SMS |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-09 : 10:09:53
|
| here is the tables and such.v_Package---------packageid int primary keymanufacturer varcharname varcharversion varcharv_Advertisement---------------packageid int advertisementname varcharcollectionid intv_Collection------------collectionid int primary keymemberclassnametable? (memberclassname table)-----------------------------name varcharIs this enough info?I basically want to join up the first 3 tables with a normal select statement, in the v_Collection table the field memberclass name's value is the name of another table, in that table there is a list of names that i need (could be multiple names per memberclassname). |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-09 : 10:58:24
|
How static is the data in the 1500 tables? Will this work?------------------------------------------------------------------------------create a table to hold combined data from the 1500 tablescreate table MemberClassName (collectionid int not null ,[name] varchar(50) not null ,Primary Key clustered (collectionid, [name]))go------------------------------------------------------------------------------paste the ouput from this statement into a new QA window --run the pasted output it to populate new tableselect distinct 'insert MemberClassName (collectionid, [name]) select distinct ' + convert(varchar, collectionid) + ', [name] from [' + memberClassName + ']' from v_collection------------------------------------------------------------------------------create a function to build a CSV string of names by collectionidif object_id('dbo.fn_namesByCollectionid') > 0 drop function dbo.fn_namesByCollectionidgocreate function dbo.fn_namesByCollectionid(@cid int)returns varchar(8000)asbegin declare @csv varchar(8000) select @csv = coalesce(@csv + ', ' + [name], [name]) from MemberClassName where collectionid = @cid return @csvend------------------------------------------------------------------------------statement to return your data using the new combined table and functionselect p.manufacturer, p.name, p.version, c.memberclassname, jerryRun = dbo.fn_namesByCollectionid(c.collectionid)from v_package pjoin v_advertisement a on p.packageid = a.packageidjoin v_collection c on a.collectionid = c.collectionidwhere a.advertisementname like 'web%'----------------------------------------------------------------------------Be One with the OptimizerTG |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2005-11-09 : 11:31:10
|
| TG you are awesome, it works like a champ.thanks so much. |
 |
|
|
|