Scotto13
asked on
Changing a query from Hard Coded to using a table list
My company has a SQL database that contains many tables - a different table for each client. There is one table that is used to identify the tables.
I wrote this query and one for each client and now want to change it to adapt to the tables listed.
select VancouverIsland.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa te,101) as postDate,categoryname, categorytable, newspaperids
from prodSQL1.Prod_Admin.dbo.Va ncouverIsl and
join prodSQL1.Prod_Admin.dbo.po stDates as p on p.id = VancouverIsland.postDateID
join prodSQL1.Prod_Admin.dbo.ca tegories as cat on cat.ID = p.CategoryID
where postdate between @StartDate and @EndDate
and IsRendered <> 2
The table - VancouverIsland is identified in a table called Categories in a field called 'CategoryTable'.
So, do I something like this?
Declare @tablename string
Set @table name = dbo.Categories.CategoryTab le
select @tablename.id, postdateid...
Please help.
Scotto13
I wrote this query and one for each client and now want to change it to adapt to the tables listed.
select VancouverIsland.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa
from prodSQL1.Prod_Admin.dbo.Va
join prodSQL1.Prod_Admin.dbo.po
join prodSQL1.Prod_Admin.dbo.ca
where postdate between @StartDate and @EndDate
and IsRendered <> 2
The table - VancouverIsland is identified in a table called Categories in a field called 'CategoryTable'.
So, do I something like this?
Declare @tablename string
Set @table name = dbo.Categories.CategoryTab
select @tablename.id, postdateid...
Please help.
Scotto13
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Change:
Set @tablename = SELECT CategoryTable from prodSQL1.Prod_Admin.dbo.Ca tegories
To:
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca tegories
Change:
where postdate between @StartDate and @EndDate
To:
where postdate between '+CAST(@StartDate AS VARCHAR(20))+' and '+CAST(@EndDate AS VARCHAR(20))
Set @tablename = SELECT CategoryTable from prodSQL1.Prod_Admin.dbo.Ca
To:
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca
Change:
where postdate between @StartDate and @EndDate
To:
where postdate between '+CAST(@StartDate AS VARCHAR(20))+' and '+CAST(@EndDate AS VARCHAR(20))
ASKER
ok. here is my updated code....
DECLARE @today datetime
DECLARE @StartDate datetime
DECLARE @EndDate Datetime
DECLARE @SQL varchar (4000)
Declare @tablename varchar (100)
SET @Today = getdate()
SET @Today = CONVERT(VARCHAR(10), @Today-7, 111)
SET @StartDate = DATEADD(DD, 2 - DATEPART(DW, @Today), @Today)
SET @EndDate = @StartDate + 6
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca tegories
set @SQL =''
select ' + @TableName + '.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa te,101) as postDate,categoryname, categorytable, newspaperids
from prodSQL1.Prod_Admin.dbo.' + @TableName + '
join prodSQL1.Prod_Admin.dbo.po stDates as p on p.id = ' + @TableName + '.postDateID
join prodSQL1.Prod_Admin.dbo.ca tegories as cat on cat.ID = p.CategoryID
where postdate between '+CAST(@StartDate AS VARCHAR(20))+' and '+CAST(@EndDate AS VARCHAR(20))'
and IsRendered <> 2''
EXEC (@SQL)
I get an error: Line 16: Incorrect syntax near '.'. this line starts: select '+TableName+'id......
DECLARE @today datetime
DECLARE @StartDate datetime
DECLARE @EndDate Datetime
DECLARE @SQL varchar (4000)
Declare @tablename varchar (100)
SET @Today = getdate()
SET @Today = CONVERT(VARCHAR(10), @Today-7, 111)
SET @StartDate = DATEADD(DD, 2 - DATEPART(DW, @Today), @Today)
SET @EndDate = @StartDate + 6
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca
set @SQL =''
select ' + @TableName + '.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa
from prodSQL1.Prod_Admin.dbo.' + @TableName + '
join prodSQL1.Prod_Admin.dbo.po
join prodSQL1.Prod_Admin.dbo.ca
where postdate between '+CAST(@StartDate AS VARCHAR(20))+' and '+CAST(@EndDate AS VARCHAR(20))'
and IsRendered <> 2''
EXEC (@SQL)
I get an error: Line 16: Incorrect syntax near '.'. this line starts: select '+TableName+'id......
ASKER
updated points...
it would be almost impossible to find this extra or missing . in your code... Just use (execute) your dynamic sql gradually, like exclude all joins and where cases and then add'em one by one until everything works. it is a lot easier for you now to find the syntax error than for us to spot it.
good luck,
yurich
good luck,
yurich
you probably have @TableName as NULL... do this...
instead of EXEC (@SQL).
PRINT @SQL and post the outcome
instead of EXEC (@SQL).
PRINT @SQL and post the outcome
And when you have found the solution, you may want to question your design, as you have discoverd it is somewhat flawed ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, change last line to:
set @SQL = @SQL + ' where postdate between '+CAST(@StartDate AS VARCHAR(20))+' and '+CAST(@EndDate AS VARCHAR(20))+' and IsRendered <> 2'
set @SQL = @SQL + ' where postdate between '+CAST(@StartDate AS VARCHAR(20))+' and '+CAST(@EndDate AS VARCHAR(20))+' and IsRendered <> 2'
Man I suck, change last line to:
set @SQL = @SQL + ' where postdate between '''+CAST(@StartDate AS VARCHAR(20))+''' and '''+CAST(@EndDate AS VARCHAR(20))+''' and IsRendered <> 2'
set @SQL = @SQL + ' where postdate between '''+CAST(@StartDate AS VARCHAR(20))+''' and '''+CAST(@EndDate AS VARCHAR(20))+''' and IsRendered <> 2'
ASKER
ok. No errors, but still no data.
I think it has to do with this row:
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca tegories
The Categories table has multiple rows (CategoryTable). How does the query know to use these one at a time.
acperkins, what do you mean that it's flawed? Do you have recomendations??
I think it has to do with this row:
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca
The Categories table has multiple rows (CategoryTable). How does the query know to use these one at a time.
acperkins, what do you mean that it's flawed? Do you have recomendations??
You need to add a WHERE clause there like:
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca tegories WHERE CategoryID = 2
SELECT @tablename = CategoryTable from prodSQL1.Prod_Admin.dbo.Ca
ASKER
ok. I had a little help from a friend and DirOrbAnt's comment. Take a look below and comment.
When this runs, it returns data. I need to know how to insert each returned set into a table. Where do I add 'Insert into tblXMLDetails' to this code below??
DECLARE @TableName varchar(255)
DECLARE @today datetime
DECLARE @StartDate datetime
DECLARE @EndDate Datetime
DECLARE @SQL varchar (4000)
SET @Today = getdate()
SET @Today = CONVERT(VARCHAR(10), @Today-7, 111)
SET @StartDate = DATEADD(DD, 2 - DATEPART(DW, @Today), @Today)
SET @EndDate = @StartDate + 6
DECLARE TableCursor CURSOR FOR
SELECT CategoryTable as TableName FROM categories
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
print @TableName + ' - Table to Query'
set @SQL = 'select TheTable.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa te,101) as postDate,categoryname, categorytable, newspaperids'
set @SQL = @SQL + ' from prodSQL1.Prod_Admin.dbo.' + @TableName + ' TheTable'
set @SQL = @SQL + ' join prodSQL1.Prod_Admin.dbo.po stDates as p on p.id = TheTable.postDateID'
set @SQL = @SQL + ' join prodSQL1.Prod_Admin.dbo.ca tegories as cat on cat.ID = p.CategoryID'
set @SQL = @SQL + ' where postdate between '''+CAST(@StartDate AS VARCHAR(20))+''' and '''+CAST(@EndDate AS VARCHAR(20))+''' and IsRendered <> 2'
print @SQL
EXEC (@SQL)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
When this runs, it returns data. I need to know how to insert each returned set into a table. Where do I add 'Insert into tblXMLDetails' to this code below??
DECLARE @TableName varchar(255)
DECLARE @today datetime
DECLARE @StartDate datetime
DECLARE @EndDate Datetime
DECLARE @SQL varchar (4000)
SET @Today = getdate()
SET @Today = CONVERT(VARCHAR(10), @Today-7, 111)
SET @StartDate = DATEADD(DD, 2 - DATEPART(DW, @Today), @Today)
SET @EndDate = @StartDate + 6
DECLARE TableCursor CURSOR FOR
SELECT CategoryTable as TableName FROM categories
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
print @TableName + ' - Table to Query'
set @SQL = 'select TheTable.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa
set @SQL = @SQL + ' from prodSQL1.Prod_Admin.dbo.' + @TableName + ' TheTable'
set @SQL = @SQL + ' join prodSQL1.Prod_Admin.dbo.po
set @SQL = @SQL + ' join prodSQL1.Prod_Admin.dbo.ca
set @SQL = @SQL + ' where postdate between '''+CAST(@StartDate AS VARCHAR(20))+''' and '''+CAST(@EndDate AS VARCHAR(20))+''' and IsRendered <> 2'
print @SQL
EXEC (@SQL)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
Replace:
set @SQL = 'select TheTable.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa te,101) as postDate,categoryname, categorytable, newspaperids'
With:
set @SQL = 'Insert into tblXMLDetails ([id], postdateid, quarkfilename, isrendered, date_posted, category, categoryid, ropadid, postDate, categoryname, categorytable, newspaperids)'
set @SQL = @SQL + ' select TheTable.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa te,101) as postDate,categoryname, categorytable, newspaperids'
set @SQL = 'select TheTable.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa
With:
set @SQL = 'Insert into tblXMLDetails ([id], postdateid, quarkfilename, isrendered, date_posted, category, categoryid, ropadid, postDate, categoryname, categorytable, newspaperids)'
set @SQL = @SQL + ' select TheTable.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa
ASKER
Sweet! Dire, thanks a lot for your help. It's been a great learning experience.
:)
ASKER
DECLARE @today datetime
DECLARE @StartDate datetime
DECLARE @EndDate Datetime
DECLARE @SQL varchar (4000)
Declare @tablename varchar (100)
SET @Today = getdate()
SET @Today = CONVERT(VARCHAR(10), @Today-7, 111)
SET @StartDate = DATEADD(DD, 2 - DATEPART(DW, @Today), @Today)
SET @EndDate = @StartDate + 6
Set @tablename = SELECT CategoryTable from prodSQL1.Prod_Admin.dbo.Ca
set @SQL =
select ' + @TableName + '.id, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDa
from prodSQL1.Prod_Admin.dbo.' + @TableName + '
join prodSQL1.Prod_Admin.dbo.po
join prodSQL1.Prod_Admin.dbo.ca
where postdate between @StartDate and @EndDate
and IsRendered <> 2'
EXEC (@SQL)