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.
Author |
Topic |
jp2code
Posting Yak Master
175 Posts |
Posted - 2009-08-19 : 15:55:49
|
I've created a temporary table containing a list of items to search for. The example below does not compile, giving this message:quote: Server: Msg 170, Level 15, State 1, Line 24Line 24: Incorrect syntax near '.'.
Here's what I'm trying to build in Query Analyzer:declare @Table table(System_ID VarChar(50))declare @delimiter nchar(1), @item_list varchar(5000), @item varchar(50), @delimiter_index int, @sysid_index int, @Date1 DateTime, @Date2 DateTimeset @delimiter=','set @item_list='chamber1,water_09,'set @Date1='07/19/2009'set @Date2='08/19/2009'-- Calculate end of first item in list:set @delimiter_index = CharIndex(@delimiter, @item_list)while (1 < @delimiter_index) begin -- when there are no items left in the list, delimiter index should be 0 set @item = Lower(SubString(@item_list, 1, @delimiter_index - 1)) -- get the leftmost item set @item_list = LTrim(SubString(@item_list, @delimiter_index + Len(@delimiter), Len(@item_list))) set @delimiter_index = CharIndex(@delimiter, @item_list) -- Calcs end of leftmost item in shortened list for next pass insert into @Table (System_ID) Values (@item)endselect system_id as 'System_ID', test_result as 'Test_Result'from ( select date_time, a.system_id, test_result from Test_Results a join @Table t on (a.system_id like t.system_id) where @Date1<Date_Time and Date_Time<@Date2union select a.date_time, Cast('Packout_' + SubString(Cast(DatePart(Year, a.[Date_Time]) AS VarChar(4)), 3, 2) AS VarChar(50)) AS a.system_id, (case Len(c.[Supervisor]) when 0 then ('Packed in ' + a.[Box_Number]) else ('Packed in ' + a.[Box_Number] + ' Override by ' + c.[Supervisor]) end) AS 'Test_Result' from (Box_Data a INNER JOIN Box_Contents c ON a.[Box_Number]=c.[Box_Number]) join @Table t on (a.system_id like t.system_id) where @Date1<a.Date_Time and a.Date_Time<@Date2) b order by date_time Once I get this working, I need to include about 6 tables and the parameter @item_list will be passed in for the application.Does someone know what I'm doing wrong? Did I forget to close a bracket or something?(FYI: If I take the '.' off of the system_id column, I get another error saying "Invalid column name") Avoid Sears Home Improvement |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-19 : 16:40:37
|
In ON-clause do not use LIKE, use =LIKE is always used when using jokers... No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
jp2code
Posting Yak Master
175 Posts |
Posted - 2009-08-19 : 16:58:32
|
Some of my passed in parameters will need to be 'AA_Decay1%'. Is LIKE required for this case?Still doesn't fix the execution problem, though. Any thoughts on that? I think I closed all of my parenthesis and such and it checks out fine until I try to execute. Avoid Sears Home Improvement |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-19 : 17:14:34
|
Now I see:Cast('Packout_' + SubString(Cast(DatePart(Year, a.[Date_Time]) AS VarChar(4)), 3, 2) AS VarChar(50)) AS a.system_idshould be:Cast('Packout_' + SubString(Cast(DatePart(Year, a.[Date_Time]) AS VarChar(4)), 3, 2) AS VarChar(50)) AS system_id No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
jp2code
Posting Yak Master
175 Posts |
Posted - 2009-08-19 : 17:21:25
|
Whoa! Look what worked: select date_time, Cast('Packout_' + SubString(Cast(DatePart(Year, date_time) AS VarChar(4)), 3, 2) AS VarChar(50)) AS system_id, (case Len(Supervisor) when 0 then ('Packed in ' + a.[Box_Number]) else ('Packed in ' + a.[Box_Number] + ' Override by ' + Supervisor) end) AS 'Test_Result' from (Box_Data as a INNER JOIN Box_Contents as c ON a.[Box_Number]=c.[Box_Number]) join @Table as t on (a.system_id = t.system_id) where @Date1<date_time and date_time<@Date2 Avoid Sears Home Improvement |
|
|
jp2code
Posting Yak Master
175 Posts |
Posted - 2009-08-19 : 17:36:46
|
system_id was not defined yet, so I had to use the equation that created the system_id in the JOIN clause.Basic mistake, but hard to find with the error message I was given! Avoid Sears Home Improvement |
|
|
|
|
|
|
|