SQLScholar
Programmer
Hey all,
I am trying to get a comma separated list of all my pk columns for a table.
So i have this
Which returns this
QuoteId,LinkId
Now if i change it to add the table table (becuase i want to find the schema of the table) to this
I now get this
LinkId
So thought it must be something to do with the join. So i ran this:
And i still get this
QuoteId
LinkId
So what is going on here? I cant work out how i am missing a record on the second query.
Any help gratefully recieved!
Dan
----------------------------------------
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch
Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
I am trying to get a comma separated list of all my pk columns for a table.
So i have this
SQL:
DECLARE @PKCols VARCHAR(100)
SELECT @PKCols = CASE WHEN @PKCols IS NULL THEN ''
ELSE @PKCols + ','
END + c.name
FROM sysindexes i
INNER JOIN sysobjects t ON i.id = t.id
INNER JOIN sysindexkeys k ON i.indid = k.indid
AND i.id = k.ID
INNER JOIN syscolumns c ON c.id = t.id
AND c.colid = k.colid
WHERE i.id = t.id
AND i.indid BETWEEN 1 AND 254
AND ( i.status & 2048 ) = 2048
AND t.id = OBJECT_ID('tblMI_QuoteAdditionalDetailsUp')
ORDER BY t.name ,
k.KeyNo
SELECT @PKCols
Which returns this
QuoteId,LinkId
Now if i change it to add the table table (becuase i want to find the schema of the table) to this
SQL:
DECLARE @PKCols VARCHAR(100)
SELECT @PKCols =
CASE WHEN @PKCols IS NULL THEN ''
ELSE @PKCols + ','
END + c.name
FROM sysindexes i
INNER JOIN sysobjects t ON i.id = t.id
INNER JOIN sysindexkeys k ON i.indid = k.indid
AND i.id = k.ID
INNER JOIN syscolumns c ON c.id = t.id
AND c.colid = k.colid
INNER JOIN sys.tables tabs ON i.id = tabs.object_id
WHERE i.id = t.id
AND i.indid BETWEEN 1 AND 254
AND ( i.status & 2048 ) = 2048
AND t.id = OBJECT_ID('tblMI_QuoteAdditionalDetailsUp')
ORDER BY t.name ,
k.KeyNo
SELECT @PKCols
I now get this
LinkId
So thought it must be something to do with the join. So i ran this:
SQL:
SELECT c.name
FROM sysindexes i
INNER JOIN sysobjects t ON i.id = t.id
INNER JOIN sysindexkeys k ON i.indid = k.indid
AND i.id = k.ID
INNER JOIN syscolumns c ON c.id = t.id
AND c.colid = k.colid
INNER JOIN sys.tables tabs ON i.id = tabs.object_id
WHERE i.id = t.id
AND i.indid BETWEEN 1 AND 254
AND ( i.status & 2048 ) = 2048
AND t.id = OBJECT_ID('tblMI_QuoteAdditionalDetailsUp')
ORDER BY t.name ,
k.KeyNo
And i still get this
QuoteId
LinkId
So what is going on here? I cant work out how i am missing a record on the second query.
Any help gratefully recieved!
Dan
----------------------------------------
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch
Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------