SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Insert record before doing a select statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bholmstrom
Yak Posting Veteran

USA
69 Posts

Posted - 12/04/2013 :  15:52:26  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4363 Posts

Posted - 12/04/2013 :  16:54:14  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 12/05/2013 :  02:02:29  Show Profile  Reply with Quote
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

United Kingdom
22403 Posts

Posted - 12/05/2013 :  05:45:47  Show Profile  Reply with Quote
"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

USA
69 Posts

Posted - 12/05/2013 :  07:50:43  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000