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
 General SQL Server Forums
 New to SQL Server Programming
 Table changed, New Query

Author  Topic 

Rawler
Starting Member

9 Posts

Posted - 2014-09-09 : 16:38:12
Hi guys,

I have this old table that was using a Stored Procedure for querying, but now that table has one new column and the query must use that new column.

My problem is: I need to keep querying that old table with the old columns until a date (when the new column was added) and after that date I have to query the new column.

My question is: Since a couple of Stored Procedures query that table, I have chosen to create an unique index for the date column and add in the WHERE clause the date until it must search and added an union with the same query but with the new column and also in the WHERE added a date to start searching from there.

So there is a better way to accomplish this? Is this way ok?

This is an example of what I tried to explain:

-- Old Query
SELECT DISTINCT ColumnA, ColumnB
FROM TableA
WHERE ColumnA IS NOT NULL AND MONTH(DateColumn) = @aMONTH AND YEAR(DateColumn) = @aYEAR

-- New Query
SELECT DISTINCT ColumnA, ColumnB
FROM TableA
WHERE ColumnA IS NOT NULL AND MONTH(DateColumn) = @aMONTH AND YEAR(DateColumn) = @aYEAR
AND TableA.DateColumn <= '08/08/2014'
UNION
SELECT DISTINCT convert(varchar(50), TableB.NewColumn), ColumnB
FROM TableA INNER JOIN TableB ON TableA.NewColumn = TableB.NewColumn
WHERE ColumnA IS NOT NULL AND MONTH(DateColumn) = @aMONTH AND YEAR(DateColumn) = @aYEAR
AND TableA.DateColumn >= '08/08/2014'

Regards,
raúl

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-09-09 : 17:50:57
Does it work and perform OK? If so, well done!
Go to Top of Page
   

- Advertisement -