|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
Generating a unique ID for each DISTINCT row
Is it possible in SQL (specifically SQL Server stored procedures) to return any kind of unique value along with each row in a SELECT DISTINCT... recordset?
I don't care what the value is, as long as there's one for each row in the recordset and each one is unique. Thanks! |
|
#2
|
||||
|
||||
|
One way to do this in your stored proc is to create a temporary table in your stored proc and make one column as an identity column. Then you can insert rows into the temp table and then select from it.
Code:
-- Create the temp table
CREATE TABLE #temp (
id INTEGER IDENTITY(1,1),
col1 type1,
col2 type2,
....
)
-- Insert rows into the temp table
INSERT INTO #temp (
col1,
col2,
...
)
SELECT DISTINCT
...
...
-- Return the rows that were selected into the temp table
SELECT * FROM #temp
-- Drop the temp table before quitting the stored proc.
DROP TABLE #temp
Another way to do this: SELECT DISTINCT newid(), rest of columns The newid() function will return unique GUIDs. Depending on what you want, you can use either way. |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > Generating a unique ID for each DISTINCT row |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|