July 5, 2019 at 7:48 am
Hi,
I've been given a spreadsheet containing a list of identity values that our marketing team need updating. I absentmindedly copied the column and pasted it into an online delimiter so I could paste this into management studio to check how many of the rows I'd be updating.
The reason I say I absentmindedly pasted the info is that I forget to remove the title from the column, when I came to run the query it gave me an, erm, unexpected result set.
This is a (heavily) truncated version of the script I ran.
SELECT * FROM MyTable AS MT
WHERE MT.CustID IN (CustID,112946,124446,670699,179255,726549,186510,186438);
So, with the CustID in the 'IN' selection the query returned every row in the table. The column is an Integer so I would have expected the query to fail. I can only something is getting messed up by an implicit conversion.
Can anyone explain what's happening here?
This is a high-class Bureau-de-Change.
July 5, 2019 at 7:57 am
I think I'm being silly here, I'm combining the column name with explicitly specified values, I didn't think that this was valid syntax. Surprisingly, to me anyway, it seems like it is.
This is a high-class Bureau-de-Change.
July 5, 2019 at 8:56 am
Don't have an answer to this - but learned something new today. I would have expected it to fail as well but tested it out and it works !
July 5, 2019 at 11:22 am
It's because CustID is a column name. Essentially your where clause is doing 1=1.
The following will fail with an "Invalid column name" error:
DROP TABLE IF EXISTS #noddy
CREATE TABLE #noddy
(NoddyIDINT IDENTITY(1,1),
NoddyStringVARCHAR(20),
NoddyUniStringNVARCHAR(20),
NoddyIntINT)
INSERT INTO #noddy (NoddyString, NoddyUniString, NoddyInt)
VALUES ('Five', 'Ten Fiftyfold', 1900),
('From', 'The Flagstones', 1901),
('Hither', 'to', 1902),
('Musette', 'and Drums', 1903)
SELECT
*
FROM
#noddy
WHERE
NoddyInt IN (1900, 1903, Hither)
But this will succeed and return all rows:
SELECT
*
FROM
#noddy
WHERE
NoddyInt IN (1900, 1903, NoddyInt)
July 5, 2019 at 12:29 pm
You must work in Toyland.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
July 5, 2019 at 1:04 pm
Can speak for anyone else, but this place feels like a circus sometimes.
This is a high-class Bureau-de-Change.
July 5, 2019 at 1:20 pm
You must work in Toyland.
Where others use variations of "foobar" for things they can't think of a name for, I use variations of "noddy". You're not the first who's commented on it. I have no idea where it came from and I'm aware that it makes no sense linguistically but I find, as with most things, I just don't care =D
July 5, 2019 at 1:44 pm
Phil Parkin wrote:You must work in Toyland.
Where others use variations of "foobar" for things they can't think of a name for, I use variations of "noddy". You're not the first who's commented on it. I have no idea where it came from and I'm aware that it makes no sense linguistically but I find, as with most things, I just don't care =D
I'm old enough to know exactly where it came from & wholeheartedly approve. My temp tables are usually called #crap. Must do better.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
July 5, 2019 at 10:01 pm
PB_BI wrote:Phil Parkin wrote:You must work in Toyland.
Where others use variations of "foobar" for things they can't think of a name for, I use variations of "noddy". You're not the first who's commented on it. I have no idea where it came from and I'm aware that it makes no sense linguistically but I find, as with most things, I just don't care =D
I'm old enough to know exactly where it came from & wholeheartedly approve. My temp tables are usually called #crap. Must do better.
My favorite temp table is #MyHead.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply