Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Exec problem

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 Optimizer
TG
Go to Top of Page

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.

TableA
Field1 Field2 Field3

Field3 has the name of the other table i need to select from.

Table? (Field3 result)
Field1

I 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?
Go to Top of Page

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 northwind
set nocount on

---------------------------------------------------
--DDL/DML
create table myTable (myTableColumn varchar(15))
create table table1 (name varchar(15))
create table table2 (name varchar(15))

go
insert myTable
select 'Table1' union
select 'table2'

insert table1 (name)
select 'tg' union all
select null

insert table1 (name)
select 'HalaszJ' union all
select null
go
---------------------------------------------------
--Get all names from all tables
declare @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 myTable
where myTablecolumn is NOT NULL

--print @sql
exec(@sql)

go

drop table myTable
drop table table1
drop table table2


Be One with the Optimizer
TG
Go to Top of Page

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 appriciated

select
p.manufacturer,
p.name,
p.version,
c.memberclassname,
' ' 'JerryReturn'
into
#JerryReturnData

from
v_package p
join v_advertisement a on p.packageid = a.packageid
join v_collection c on a.collectionid = c.collectionid
where
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 @JerryReturn

declare @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
end

close crs_JerryReturn
deallocate crs_JerryReturn
Go to Top of Page

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 Optimizer
TG
Go to Top of Page

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.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-11-08 : 14:48:40
>>select distinct memberclassname from #JerryReturnData
So this returns a few thousand rows? or less than 50?


Be One with the Optimizer
TG
Go to Top of Page

HalaszJ
Yak Posting Veteran

59 Posts

Posted - 2005-11-08 : 14:55:36
few thousand, well... i think its actually 1500
Go to Top of Page

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 does

Please read the hint link in my sig, post some of the requested info, and we'll get you an answer asap

Right now it's just a guessing game



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

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.
Go to Top of Page

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 Optimizer
TG
Go to Top of Page

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
Go to Top of Page

HalaszJ
Yak Posting Veteran

59 Posts

Posted - 2005-11-09 : 10:09:53
here is the tables and such.

v_Package
---------
packageid int primary key
manufacturer varchar
name varchar
version varchar


v_Advertisement
---------------
packageid int
advertisementname varchar
collectionid int

v_Collection
------------
collectionid int primary key
memberclassname


table? (memberclassname table)
-----------------------------
name varchar


Is 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).
Go to Top of Page

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 tables
create 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 table
select 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 collectionid
if object_id('dbo.fn_namesByCollectionid') > 0
drop function dbo.fn_namesByCollectionid
go
create function dbo.fn_namesByCollectionid(@cid int)
returns varchar(8000)
as
begin
declare @csv varchar(8000)
select @csv = coalesce(@csv + ', ' + [name], [name])
from MemberClassName
where collectionid = @cid

return @csv
end

----------------------------------------------------------------------------
--statement to return your data using the new combined table and function
select p.manufacturer,
p.name,
p.version,
c.memberclassname,
jerryRun = dbo.fn_namesByCollectionid(c.collectionid)
from v_package p
join v_advertisement a on p.packageid = a.packageid
join v_collection c on a.collectionid = c.collectionid
where a.advertisementname like 'web%'
----------------------------------------------------------------------------


Be One with the Optimizer
TG
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -