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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Insert record before doing a select statement

Author  Topic 

bholmstrom
Yak Posting Veteran

76 Posts

Posted - 2013-12-04 : 15:52:26
Good afternoon,

We need to insert a row into a table before a select statement.

The entry would be '1 ,'--select--,'--select--','1'
This would be the first entry in the completed query.

Is this possible?

Here is the current query:

SELECT 0 as RowIndex, NC_DisplayName,NC_DomainAndUser,NC_TestUser
from NCOS_DomainUser union all
Select 1 ,'Other','Other1','1'
order by RowIndex, NCOS_DomainUser.NC_DisplayName

Thank you in advance

Bryan Holmstrom

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-12-04 : 16:54:14
I'm not sure I follow. Do you want insert a row every time you query the table or are you looking to insert a row once, if it doesn't exist or something else?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-05 : 02:02:29
do you mean this?


SELECT 0 as RowIndex, NC_DisplayName,NC_DomainAndUser,NC_TestUser ,1 AS Ord
from NCOS_DomainUser union all
Select 1 ,'Other','Other1','1',0
order by Ord,RowIndex, NCOS_DomainUser.NC_DisplayName

But IMO you dont need to be concerned on which order you store the data in the table. you can always retrieve it in order you want by adding an additional Ord field as shown above and use it in ORDER BY while selecting

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2013-12-05 : 05:45:47
"This would be the first entry in the completed query."

Change the RowIndex so the first SELECT is 2 and then second one is 1? Then the second SELECT will be ordered before the first one.
Go to Top of Page

bholmstrom
Yak Posting Veteran

76 Posts

Posted - 2013-12-05 : 07:50:43
Thank you everyone for your input. Heres how I solved it.

SELECT -1 as RowIndex,'--select--' ,'--select--' ,0
SELECT 0 as RowIndex, NC_DisplayName,NC_DomainAndUser,NC_TestUser from NCOS_DomainUser union all
Select -1 ,'--select--','--select--','0'
union all
Select 1 ,'Other','Other1','0'
union all
Select 1 ,'Other','Other1','1'
order by RowIndex, NCOS_DomainUser.NC_DisplayName

Bryan Holmstrom
Go to Top of Page
   

- Advertisement -