| Show Results When more than just my result shows up. Posted: 17 Apr 2013 01:06 AM PDT Hey Guys, I'm trying to Show All location changes in our system in the last 5 days, now all active items are stored in a table called OITM and when updated the last iteration of the item is stored in a table called AITM however this can be any change to the product, not just a location change. Which means that I can have the same location show up multiple times. The second issue is that the update date is stored on the actual item and the previous update then moves to AITM however if there are 2 changes during this period I would like them all to show up as well. Here is a [url=http://sqlfiddle.com/#!3/22869/1] SQL FIDDLE[/url] of what I have so far, otherwise I will show the tables below:[code="sql"] CREATE TABLE AITM ([ItemCode] varchar(9), [FrgnName] varchar(11), [UpdateDate] datetime, [LogInstanc] int); INSERT INTO AITM ([ItemCode], [FrgnName], [UpdateDate], [LogInstanc])VALUES ('1513360GD', 'STACK-105', '2012-12-30 00:00:00', 1), ('1513360GD', 'STACK-105', '2013-04-12 00:00:00', 2), ('SEW-3035', NULL, '2013-03-21 00:00:00', 1), ('SEW-3035', NULL, '2013-04-13 00:00:00', 2), ('SEW-3035', 'D-34-35-B-M', '2013-04-14 00:00:00', 3), ('SEW-3035', 'b-13-b', '2013-04-15 00:00:00', 4), ('SEW-3035', 'B-13-B', '2013-04-15 00:00:00', 5);CREATE TABLE OITM ([ItemCode] varchar(9), [FrgnName] varchar(6), [UpdateDate] datetime, [LogInstanc] int); INSERT INTO OITM ([ItemCode], [FrgnName], [UpdateDate], [LogInstanc])VALUES ('1513360GD', 'FW-66', '2013-04-15 00:00:00', 0), ('SEW-3035', 'B-13-B', '2013-04-16 00:00:00', 0); [/code]And here is my current code:[code="sql"]SELECT DISTINCT T0.ItemCode, T1.FrgnName as [From Location], T0.FrgnName as [To Location], T0.UpdateDateFROM OITM T0 Left JOIN (Select ItemCode, FrgnName, UpdateDate From AITM A Where LogInstanc in (Select Max(LogInstanc) From AITM B Where A.ItemCode = B.ItemCode) Group By ItemCode, FrgnName, UpdateDate) as T1 ON T0.ItemCode = T1.ItemCode WHERE ISNULL(T0.FrgnName, 0) <> ISNULL(T1.FrgnName, 0)Group By T0.ItemCode, T1.FrgnName, T0.FrgnName, T0.UpdateDateHaving Max(T0.UpdateDate) > GETDATE()-4ORDER BY 4, 1[/code]The results now show like this:[code="other"]ITEMCODE FROM LOCATION TO LOCATION UPDATEDATE1513360GD STACK-105 FW-66 April, 15 2013 00:00:00+0000[/code]I would like the results to show all changes within that period only if the location changed as such:[code="other"]ITEMCODE FROM LOCATION TO LOCATION UPDATEDATE1513360GD STACK-105 FW-66 April, 15 2013 00:00:00+0000SEW-3035 NULL D-34-35-B-M April, 13 2013 00:00:00+0000SEW-3035 D-34-35-B-M B-13-B April, 14 2013 00:00:00+0000[/code] |
| Stored Procedure execution with parameters Posted: 16 Apr 2013 10:24 PM PDT While checking our production plan cache, I noticed that over half of the entries were like the following:exec mt_amstask7 'AMS','78609072','1045458320','20130417 05:05','AMH','20130417 05:07','U'exec mt_amstask7 'AMS','78609072','1045458304','20130417 05:05','AMH','20130417 05:07','U'exec mt_amstask7 'AMS','78609072','1045458320','20130417 05:05','AMH','20130417 05:06','U'etc.Of the 20,563 cached plans, 11,449 were of this type. It is obvious to me that the only difference is the value(s) of the parameters.My question: Is it possible to execute a stored procedure with parameters using sp_executesql? I haven't been able to make this work. I've tried this:DECLARE @SQL NVARCHAR(2048);DECLARE @SQLParms NVARCHAR(2048);DECLARE @SQLParm1 INT;DECLARE @Parm1 INT;DECLARE @SQLParm2 VARCHAR(10);DECLARE @Parm2 VARCHAR(10);DECLARE @SQLParm3 DATETIME;DECLARE @Parm3 DATETIME;SET @Parm1 = 1;SET @Parm2 = 'ABC';SET @Parm3 = '1958-11-14 04:25';SET @SQL = N'TestProc @SQLParm1=@Parm1,@SQLParm2=@Parm2,@SQLParm3=@Parm3;';EXECUTE sys.sp_executesql @SQL,@SQLParms,@SQLParm1,@SQLParm2,@SQLParm3;Msg 137, Level 15, State 2, Line 1Must declare the scalar variable "@Parm1".I'm thinking the DECLAREd parameters are out of scope for the execution.Any help would be appreciated.~ Jeff |