0

I need to insert a default value to my select statement So I am doing it in this fashion.

Is there any better way I can do this?

I need to insert "All" as my first row value to table Sitetbl select statement.

 DECLARE @SiteValue TABLE  
 (  
    Id INT IDENTITY(1,1),  
    SiteID Nvarchar(15),  
    SiteName Nvarchar(100)  
 )  

 Insert into  @SiteValue(SiteID,SiteName) values('All','All')

 insert into  @SiteValue(SiteID,SiteName) 

 SELECT  DISTINCT  SiteID,SiteName from Sitetbl

Ex: Result how I am looking

All  All
St   Singapore
IN   India
CH   China

3 Answers 3

1

You can simplify it like following.

insert into @SiteValue(SiteID,SiteName)
SELECT 'All' SiteID,'All' SiteName 
UNION
SELECT DISTINCT SiteID,SiteName from Sitetbl
Sign up to request clarification or add additional context in comments.

Comments

0

No. There are other ways, such as using a UNION ALL in your SELECT, but they are not "better" than the way you are doing it now.

Comments

0

Perhaps, introduction of the view:

CREATE VIEW SitetblWithDefault
AS
SELECT 'All' SiteID,'All' SiteName 
UNION ALL
SELECT  SiteID,SiteName from Sitetbl

So, all selects of this view will have Default value just before other values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.