Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Select table name dynamically

$
0
0

I want to create a cursor in which the select statement should be
dynamic:
<o:p></o:p>

if @PhoneType = White<o:p></o:p>

Then the select statement should
be as<o:p></o:p>

selectdistinct
MODEL_NUM from Dbo.Apple<o:p></o:p>

if @PhoneType = Black<o:p></o:p>

selectdistinct
MODEL_NUM from Dbo.Samsung<o:p></o:p>


---------------------

     IF@MobileId ISNOTNULL

         BEGIN

             IF@PhoneType ='white'

                    SET@SqlStmt ='select distinct MODEL_NUM from dbo.Apple'

             IF@PhoneType ='Black'

                    SET@SqlStmt ='select distinct MODEL_NUM from dbo.Samsung'

             declareMOBILE cursorfor

                    selectdistinct MODEL_NUM fromDbo.Apple


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>