Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Paramater Concatenation


Paramater Concatenation

Author
Message
ksrikanth77
ksrikanth77
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 249
Hi Everyone,

DECLARE @CN VARCHAR (100)
SET @CN = 'abc pvt Ltd'
select * from [@CN'+'$Production]


My output should be select * from [abc pvt Ltd$Production].I DO NOT want to use a string for my Select Query

Could anyone of you help on me this.
Andrew Kernodle
Andrew Kernodle
SSChasing Mays
SSChasing Mays (610 reputation)SSChasing Mays (610 reputation)SSChasing Mays (610 reputation)SSChasing Mays (610 reputation)SSChasing Mays (610 reputation)SSChasing Mays (610 reputation)SSChasing Mays (610 reputation)SSChasing Mays (610 reputation)

Group: General Forum Members
Points: 610 Visits: 8135
As far as I know of, without building your query as a string and passing it to EXEC or (preferably) sp_executesql, this isn't possible, to the best of my knowledge.

If I remember correctly, it has something to do with requiring database names in queries to be explicit declarations; you can accomplish this with a string that gets run through EXEC, since the variable is replaced by its value, but not in a non-EXEC query.

- :-D
Alan.B
Alan.B
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2236 Visits: 7418
You can include table names or derived tables in your from clause but not a non-static value. Dynamic SQL (using sp_executesql not EXEC) is the way to go for this.There is no problem with Dynamic SQL when it done correctly (e.g. in a way where SQL Injection can't happen).

That said, here is an alternative solution if you MUST avoid Dynamic SQL.


--sample data
IF OBJECT_ID('tempdb..#tbl1') IS NOT NULL DROP TABLE #tbl1;
IF OBJECT_ID('tempdb..#tbl2') IS NOT NULL DROP TABLE #tbl2;
IF OBJECT_ID('tempdb..#tbl3') IS NOT NULL DROP TABLE #tbl3;
IF OBJECT_ID('tempdb..#tbls') IS NOT NULL DROP TABLE #tbls;

SELECT 't1' AS tbl INTO #tbl1;SELECT 't2' AS tbl INTO #tbl2;SELECT 't3' AS tbl INTO #tbl3;

-- catch-all query
WITH tbls(tbl) AS (SELECT '#tbl1' UNION SELECT '#tbl2' UNION SELECT '#tbl3')
SELECT * INTO #tbls FROM tbls
GO

DECLARE @CN VARCHAR (100)='#tbl1';

IF EXISTS (SELECT tbl FROM #tbls WHERE tbl=@CN)
BEGIN
   IF @CN='#tbl1'
      SELECT * FROM #tbl1
      --SELECT TOP 10 * FROM #tbl1
   IF @CN='#tbl2'
      SELECT * FROM #tbl2
      --SELECT TOP 10 * FROM #tbl2
   IF @CN='#tbl3'
      --SELECT TOP 10 * FROM #tbl3
      SELECT * FROM #tbl3
END
ELSE
   SELECT 'table does not exist or has not been entered into #tbls. Fire someone.' AS Msg



Here I am using a catch-all query; see this article by Gail Shaw for more information about catch-all queries...

One final important note

Do you really need all the rows from any table defined by @CN? Even if it has a couple billion rows?
I would suggest that you have the query return a limited number of values (note my commented out code above)...

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search