Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing the single quotes for columns in select statement in SQL Server
    primarykey
    data
    text
    <p>I have 2 tables. In <code>table1</code> I have some rows for persons like this:</p> <ol> <li>PersonX - ID</li> <li>PersonX - Name</li> <li>PersonX - Address</li> <li>PersonY - ID</li> <li>PersonY - AGE</li> </ol> <p>In 2nd table, above mentioned <code>ID, NAME,ADDRESS,AGE</code> will be columns. And we have detailed data of personX and PersonY here.</p> <p>Now, main issue is in stored procedure, using cursor, I am storing <code>table1</code> values (<code>'ID', 'Name', ...</code>) in a variable <code>@Element</code>.</p> <p>Now I am using select statement in same cursor as below:</p> <pre><code>SELECT @Element From Table2 </code></pre> <p>I need output of user details like his id, age, address etc. But instead I am getting output as <code>'ID', 'NAME', 'AGE'</code> etc....</p> <p>I found that this is because <code>@Element</code> is <code>varchar</code> and has string value, so select statement is executed as below:</p> <pre><code>SELECT 'ID' from table2. </code></pre> <p>but all I need is like below </p> <pre><code>SELECT ID FROM TABLE2 </code></pre> <p>I used replace function its not working for me. Case function, I can't use it because we can't say what data is there for a person in <code>table1</code>. It varies. I need one dynamic statement which can be use for all records. instead of executing case for each record.</p> <pre><code>SELECT REPLACE(@Element,'''','') FROM TABLE2 </code></pre> <p>(Still getting 'ID' as output, instead of corresponding value in Table2)</p> <p>Please help me in this. Hope you understand my explanation </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload