![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a query which works fine when hardcoded which is as follows SELECT SchemaFields.SchemaID, SchemaFields.ObjectClass, SchemaFields.ObjectType, 'New' AS ActionType, DataQuery.Count FROM SchemaFields LEFT OUTER JOIN (SELECT [01-RecordClass], [02- RecordAction], COUNT(*) AS Count FROM TestData6061.dbo.accounting WHERE (AutomationType = 'Loader') AND ([Negative Testcase] = 0) GROUP BY [01-RecordClass], [02- RecordAction]) AS DataQuery ON DataQuery.[01-RecordClass] = SchemaFields.ObjectType AND DataQuery.[02-RecordAction] = 'New' WHERE (SchemaFields.SchemaID = 133) GROUP BY SchemaFields.SchemaID, SchemaFields.ObjectClass, SchemaFields.ObjectType, DataQuery.Count In the above query 'TestData6061', 'accounting' (from the 2nd select statement) and 'SchemaID' are all going to be dynamic. Is there anyway I can generalize this using functions/stored procedures etc |
#3
| |||
| |||
|
|
I have a query which works fine when hardcoded which is as follows SELECT SchemaFields.SchemaID, SchemaFields.ObjectClass, SchemaFields.ObjectType, 'New' AS ActionType, DataQuery.Count FROM SchemaFields LEFT OUTER JOIN (SELECT [01-RecordClass], [02- RecordAction], COUNT(*) AS Count FROM TestData6061.dbo.accounting WHERE (AutomationType = 'Loader') AND ([Negative Testcase] = 0) GROUP BY [01-RecordClass], [02- RecordAction]) AS DataQuery ON DataQuery.[01-RecordClass] = SchemaFields.ObjectType AND DataQuery.[02-RecordAction] = 'New' WHERE (SchemaFields.SchemaID = 133) GROUP BY SchemaFields.SchemaID, SchemaFields.ObjectClass, SchemaFields.ObjectType, DataQuery.Count In the above query 'TestData6061', 'accounting' (from the 2nd select statement) and 'SchemaID' are all going to be dynamic. Is there anyway I can generalize this using functions/stored procedures etc |
#4
| |||
| |||
|
|
I am sorry there are not 3 dynamic parameters, but 'accounting' is the only parameter which is dynamic. accounting is one of the tables under TestData6061 and there are bunch of others so I want to have a query which will give me table names for 'TestData6061' in the above example |
#5
| |||
| |||
|
|
Vic (vikra... (AT) gmail (DOT) com) writes: I am sorry there are not 3 dynamic parameters, but 'accounting' is the only parameter which is dynamic. accounting is one of the tables under TestData6061 and there are bunch of others so I want to have a query which will give me table names for 'TestData6061' in the above example Generally, when there are many tables with the same schema which makes you long for being able to parameterise the table name, the general opinion is that there is a flaw in the design. A table is intended to describe a unique identity, and therefore all tables are expected to have a unique set of columns. And thus there would be little reason to parameterise table names - and therefore there is no direct feature to do this. If you give more details on why you have all these similar tables, we can give ideas on how to improve the design. Or, if you are stuck with it, what possible workarounds there may be. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#6
| |||
| |||
|
|
Thanks Erland for the reply first of all. The thing is like I didn't really design the system. So here is what I came up with so far. |
#7
| |||
| |||
|
|
Thanks Erland for the reply first of all. The thing is like I didn't really design the system. So here is what I came up with so far. I have a store procedure to which I pass a number which will give me a particular entry in that database. say if I type exec SP_FETCH_ROWS 1 its going to return accounting or if I pass 4 its going to return calendar etc |
|
if @Count = 1 or @Count = 4 or @Count = 7 or @Count = 8 or @Count >= 14 or @Count = 17 or @Count = 18 or @Count = 22 or @Count = 25 or @Count = 27 or @Count = 28 or @Count = 29 exec releases.dbo.SP_FETCH_ROWS @Count end which isn't working though. The above loop ideally will give me list of items at those specific positions in TestData6061.INFORMATION_SCHEMA.Tables |
#8
| |||
| |||
|
|
Vic (vikra... (AT) gmail (DOT) com) writes: Thanks Erland for the reply first of all. The thing is like I didn't really design the system. So here is what I came up with so far. I have a store procedure to which I pass a number which will give me a particular entry in that database. say if I type exec SP_FETCH_ROWS 1 its going to return accounting or if I pass 4 its going to return calendar etc First: don't name your procedures sp_something, as the sp_ prefix is reserved for system procedures. Next, I don't understand what this SP_FETCH_ROWS is supposed to achieved. It returns a certain table name given its position in the current collation, but I don't see what this could be useful for, neither in general, nor in relation to the problem in your original post. if @Count = 1 or @Count = 4 or @Count = 7 or @Count = 8 or @Count >= 14 or @Count = 17 or @Count = 18 or @Count = 22 or @Count = 25 or @Count = 27 or @Count = 28 or @Count = 29 exec releases.dbo.SP_FETCH_ROWS @Count end which isn't working though. The above loop ideally will give me list of items at those specific positions in TestData6061.INFORMATION_SCHEMA.Tables And then someone adds accounting_bck for some reason and your numbers get out of sync? -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#9
| |||
| |||
|
|
We have another set of tables like 'TestData6061' or 'TestData606' etc where we have some data. |
|
So for running the reports we basically want to pass that 'accounting', 'fundstructures' etc and not hard code it. |
#10
| |||
| |||
|
|
Vic wrote: We have another set of tables like 'TestData6061' or 'TestData606' etc where we have some data. This sounds badly designed. Assuming that these tables look something like this: [TestData6061] CustomerID | FirstName | LastName | TestResult -----------+-----------+----------+----------- 1 | Winona | Lord | BTN 2 | Billie | Keister | XRC 3 | Daren | Koster | RNC 4 | Valentine | Perkins | FNG 5 | Aleta | Geyer | RWC [TestData6062] CustomerID | FirstName | LastName | TestResult -----------+-----------+----------+----------- 6 | Bronte | Dunlap | SSC 7 | Sinclair | Basmanoff| RRE 8 | Monty | Siegrist | RBN 9 | Suzie | Bailey | NNN 10 | Shantel | Powers | BNM they should be replaced with a single table like this: [TestData] TestID | CustomerID | FirstName | LastName | TestResult -------+------------+-----------+----------+----------- 0601 | 1 | Winona | Lord | BTN 0601 | 2 | Billie | Keister | XRC 0601 | 3 | Daren | Koster | RNC 0601 | 4 | Valentine | Perkins | FNG 0601 | 5 | Aleta | Geyer | RWC 0602 | 6 | Bronte | Dunlap | SSC 0602 | 7 | Sinclair | Basmanoff| RRE 0602 | 8 | Monty | Siegrist | RBN 0602 | 9 | Suzie | Bailey | NNN 0602 | 10 | Shantel | Powers | BNM So for running the reports we basically want to pass that 'accounting', 'fundstructures' etc and not hard code it. This is the confusing bit. What sort of procedure do you have that is equally able to operate on 'accounting' and 'fundstructures' and several other things? Unlike TestData0601 and TestData0602, they don't sound like they have similar structures. Are you doing a lot of SELECT * stuff? If so, then that's another thing that may be good to revise. |
![]() |
| Thread Tools | |
| Display Modes | |
| |