I have a problem returning records in a sproc. No error is thrown but I think data coming from one server environment is not mixing well in another.
set @shipedon = YEAR(@shipdate) * 10000 + MONTH(@shipdate) * 100 + DAY(@shipdate)
This works:
SELECT [ITEM_KEY],
max([REVISION]) Rev
FROM SERVER2.[BOMSystem].[dbo].[ITEM]
WHERE ITEM_KEY collate DATABASE_DEFAULT in
('391000180', '391000189', '391000191', '391000201',
'391000214', '391000215', '391000216', '391000226')
AND DATE_EFF_FROM <= @shipedon
GROUP BY ITEM_KEY
shows 8 rows like:
391000180 0001
391000189 0001
391000191 0001
This fails:
SELECT [ITEM_KEY],
max([REVISION]) Rev
INTO #rev
FROM SERVER2.[BOMSystem].[dbo].[ITEM]
WHERE ITEM_KEY collate DATABASE_DEFAULT in (@items)
AND DATE_EFF_FROM <= @shipedon
GROUP BY ITEM_KEY
SELECT * from #rev shows no results.
SELECT @items = SUBSTRING(
(SELECT distinct ',' +''''+ ltrim(rtrim(ItemNumber )) +''''
collate DATABASE_DEFAULT
FROM #ShipTemp
FOR XML PATH('')),2,20000)
You can also use the database_default option in the COLLATE clause to specify that a column in a temporary table use the collation default of the current user database for the connection instead of tempdb. Casting the collation of an expression.
As with database definitions, you can add the COLLATE clause when you define a column of characters. This allows you to apply a specific collation to the data in the column without affecting the rest of the database.
According the SQL Server Books Online, the characters included in range searches depend on the sorting rules of the collation. Collation Latin1_General_CS_AS uses dictionary order so both upper and lower case characters of the specified range are included.
Collations in SQL Server provide sorting rules, case, and accent sensitivity properties for your data. Collations that are used with character data types, such as char and varchar, dictate the code page and corresponding characters that can be represented for that data type.
There is no need to concatenate all of them into a comma delimited string.
IN
accepts a sub query that returns a single column of items to be tested. Just use
SELECT [ITEM_KEY],
max([REVISION]) Rev
INTO #rev
FROM SERVER2.[BOMSystem].[dbo].[ITEM]
WHERE ITEM_KEY COLLATE DATABASE_DEFAULT IN (SELECT LTRIM(ItemNumber )
FROM #ShipTemp)
AND DATE_EFF_FROM <= @shipedon
GROUP BY ITEM_KEY
Also no need to RTRIM
as trailing spaces aren't significant in a comparison and if ItemNumber
is numeric you should use a numeric datatype, not a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With