.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Sql Server >> Post New QuestionBookmark and Share Subscribe to Forum

Select Query in sql server

Posted By: Aamrapali Tamgadge     Posted Date: March 05, 2013    Points:5   Category :Sql Server
Hello in my table there are 3 addresses but i want to show them by combining them in to one address seperated by comma. In simple select query it is working but how to execute it in dynamic.i have written follwing.

if @MODE='FIND'

Select @sql='SELECT tbl_SocietyMaster.socaddr1 + tbl_SocietyMaster.socaddr2 +tbl_SocietyMaster.socaddr3 AS "Address" From Mytable'

EXEC sp_executesql @sql

Responses
Author: SailajaReddy             
Posted Date: March 06, 2013     Points: 20   

BEGIN
DECLARE @SocietyMaster AS TABLE
(
socaddr1 VARCHAR(100),
socaddr2 VARCHAR(100),
socaddr3 VARCHAR(100)
)

INSERT INTO @SocietyMaster VALUES('John - addr1','John - addr2','John - addr3')
INSERT INTO @SocietyMaster VALUES('Ram - addr1','Ram - addr2','Ram - addr3')

SELECT socaddr1 + ' , ' + socaddr2 + ' , ' + socaddr3 FROM @SocietyMaster

END
GO




Author: Aamrapali Tamgadge             
Posted Date: March 06, 2013     Points: 20   

hello sir how can i write in

Select @sql='SELECT tbl_SocietyMaster.socaddr1 + tbl_SocietyMaster.socaddr2 +tbl_SocietyMaster.socaddr3 AS "Address" From tbl_SocietyMaster'

becoz i have to select more fields also that i havnt mention in this and also have to use join also so i only need to retrive from this line .

Software Programmer At OTIS ISRC
Author: SailajaReddy             
Posted Date: March 06, 2013     Points: 20   

BEGIN
DECLARE @SocietyMaster AS TABLE
(
Name VARCHAR(100),
socaddr1 VARCHAR(100),
socaddr2 VARCHAR(100),
socaddr3 VARCHAR(100)
)
DECLARE @CombinedAddr VARCHAR(350)
INSERT INTO @SocietyMaster VALUES('John' ,'John - addr1','John - addr2','John - addr3')
INSERT INTO @SocietyMaster VALUES('Ram', 'Ram - addr1','Ram - addr2','Ram - addr3')

SELECT @CombinedAddr = socaddr1 + ' , ' + socaddr2 + ' , ' + socaddr3 FROM @SocietyMaster WHERE Name ='john'
PRINT @CombinedAddr


END
GO

if it does not meet ur requirements ,plz do explain what exactly u mean dynamic???


Author: Deco             
Posted Date: March 20, 2013     Points: 20   

You can very well write the direct select query instead of using the dynamic query. dynamic query will create execution plan every time it is being executed. but the normal select query will use the execution plan from the cache.

Regards,
Deco
Author: ashleyturnier             
Posted Date: October 11, 2013     Points: 20   

The SELECT Query is performed most of the applications.

Query is:

SELECT * FROM TABLE NAME WHERE COLUMN NAME=VALUE



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend