How do I write a set based query? I have a groupSets table with fields
setId, idField, datasource, nameField, prefix, active
Data:
1,someIDfield, someTable, someField, pre1, 1
2,someotherIDfield, someTable, someotherField, pre2, 1
3,somethirdIDfield, someTable, somethirdField, pre3, 1
4,somefourthIDfield, someotherTable, somefourthField, pre4, 1
I need to generate records in another table by constructing queries from the
data in groups sets. I need to insert a record for each distinct result of
the query.
Example:
SELECT DISTINCT someIDfield FROM someTable WHERE someIDfield IS NOT NULL
then I need to do an insert for each result of the above query
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result1)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result2)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result3)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result4)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, resultN)
--next reord from groupSets
SELECT DISTINCT someotherIDfield FROM someTable WHERE someotherIDfield IS
NOT NULL
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result1)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result2)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result3)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result4)
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, resultN)
I basically want to do the same operation on each record in the groupSets
table. How can I accomplish this without a cursor? Any ideas?
Thanks for your help,
~ck
"--CELKO--" <jc*******@earthlink.netwrote in message
news:32**********************************@m3g2000h sc.googlegroups.com...
>>Can you use a variable for the FOR clause in a cursor? <<
No. But a better question is why you are using a cursor at all.
Given OLAP functions, CASE expressions, etc. in current Standard SQL,
you should only use 1-2 in your entire career at most.
>>Is there a better way? I know everybody hates cursors, but I am just
curious if this would work. <<
Well, is there a WORSE way? Dynamic SQL declaring cursors is a
nightmare on soooo many levels. Saying SQL people hate cursors is a
mild understatement. What a cursor says is that either the product or
the programmer is so weak that we have to throw away 30 years of RDBMS
and revert to low-level procedural code. And 99.98% of the time when
you see dynamic SQL, it is an admission that the programmer never had
a basic software engineering course and does not know about cohesion.
What is the actual problem you are trying to solve? Or was this a "If
I poison my cattle, will they die?" kind of question?