July 24, 2012 at 9:48 am
avishwithu (7/24/2012)
Thanks Lynn.I was just curious, how could it not work ..
Avinash
Also, I thought I'd show you another way to write your sql dynamically, not the it is needed since there is nothing requiring it in the sql code.
CREATE TABLE VW_RPT_DATA (county varchar(100), rt varchar(100),incident_date datetime)
INSERT INTO VW_RPT_DATA values('a','b',GETDATE()),('a','b',GETDATE()-400)
CREATE TABLE VW_HIST_DATA (Rt varchar(100),rtnum varchar(100))
INSERT INTO VW_HIST_DATA VALUES ('b','rtnum'),('check','rtnum2')
GO
DECLARE @rnum varchar(100)='rtnum';
DECLARE @county varchar(100) = 'a';
DECLARE @year varchar(10) = '2012';
SELECT
a.incident_date,
a.county,
a.rt,
b.rt,
b.rtnum
FROM
VW_RPT_DATA a
INNER JOIN VW_HIST_DATA b
on a.RT = b.Rt
WHERE
b.rtnum = @RNum
AND a.county = @County
AND a.incident_date >= dateadd(yy, @Year - 1900, 0)
AND a.incident_date < dateadd(yy, @Year - 1900 + 1, 0);
go
DECLARE @rnum varchar(100) = 'rtnum';
DECLARE @county varchar(100) = 'a';
DECLARE @year varchar(10) = '2012';
declare @SQLCmd nvarchar(max),
@SQLParams nvarchar(max);
set @SQLCmd =
N'SELECT
a.incident_date,
a.county,
a.rt,
b.rt,
b.rtnum
FROM
VW_RPT_DATA a
INNER JOIN VW_HIST_DATA b
on a.RT = b.Rt
WHERE
b.rtnum = @RNum
AND a.county = @County
AND a.incident_date >= dateadd(yy, @Year - 1900, 0)
AND a.incident_date < dateadd(yy, @Year - 1900 + 1, 0);';
set @SQLParams = N'@rnum varchar(100), @county varchar(100), @year varchar(10)';
exec sp_executesql @stmt = @SQLCmd, @params = @SQLParams, @rnum = 'rtnum', @county = 'a', @Year = '2012';
go
drop table VW_RPT_DATA;
drop table VW_HIST_DATA;
go
July 24, 2012 at 9:49 am
avishwithu (7/24/2012)
Thanks Lynn.I was just curious, how could it not work ..
Avinash
Sorry, not quite sure what you are asking here.
July 24, 2012 at 9:57 am
I will go with Sean, the best way is to un-do with dynamic SQL. If your code is not exposed and free from SQL Injections - convert your query as suggested by Sean.
~ Lokesh Vij
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
July 24, 2012 at 9:57 am
oh, I meant that with the initial post ,which says the dynamic sql didn't work when it actually does ..
Avinash
~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one :ermm:
July 24, 2012 at 10:07 am
Ajdba (7/24/2012)
Right function is not getting the data; in the table it is datetime but I converted to varchar(10) in the view like this...CONVERT(VARCHAR(10),I.DateOfIncidentDte, 110 ) as incident_date
Thanks
Keep the datetime columns as datestime columns.
July 24, 2012 at 10:58 am
Lynn Pettis (7/24/2012)
Ajdba (7/24/2012)
Right function is not getting the data; in the table it is datetime but I converted to varchar(10) in the view like this...CONVERT(VARCHAR(10),I.DateOfIncidentDte, 110 ) as incident_date
Thanks
Keep the datetime columns as datestime columns.
To elaborate, by converting DateOfIncidentDte to a character field, you prevent any queries using this view from using an index (if it exists) on this column in the underlying table.
July 24, 2012 at 11:14 am
thanks for the explanation.
~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one :ermm:
July 25, 2012 at 10:42 am
Hello
can you please look the execution plan and let me know if you see anything I can do to improve the query performance; I would appreciate it so much
I am not too familiar with query execution plan
Thanks
July 25, 2012 at 10:52 am
Will also need the DDL for the views/tables involved including indexes defined on the tables, the code you are running.
Viewing 9 posts - 16 through 23 (of 23 total)
You must be logged in to reply to this topic. Login to reply