Loading ...
Sorry, an error occurred while loading the content.

Query in MS SQL Server

Expand Messages
  • nilesh babar
    Hi Friends, I have one query, im using MS SQL Server 2000. I have tewo tables tblx and tbly, in tblx two columns cid and name. in tbly what are the column
    Message 1 of 3 , May 7, 2004
    • 0 Attachment
      Hi Friends,
      I have one query, im using MS SQL Server 2000.
       
      I have tewo tables tblx and tbly, in tblx two columns cid and name. in tbly what are the column names are available those column names are in tblx name field. 
      In tbly table there are some records, i want these records but only one column at a time. For that picking one columns details from table i have created one variable, so what value in that variable (meanse column name) i want to select from tbly. 
      For that i have using following query but i didn't get output as per my requirement,so please help me.
      Table Details
      tblx
              cid            name
               1              BASIC
               2              HRA
               3              MONTHLY
      tbly
              cid     employeeno   BASIC    HRA    MONTHLY
                1          1202            5000       2000   7000
                2          1658            6000       3000   9000
                3          0562            9000       5000   14000
       
      query
       
      declare @cln varchar(50)
      declare @cd as int
      declare @mcd as int
      select @cd=min(cid) from tblx
      select @mcd=max(cid) from tblx
      while (@cd)<= @mcd
       begin
        select @cln=name from tblx where cid=@cd
        select @cln from tbly
        print @cln
        select @cd=@cd+1
       end
       
       
      Please reply me if anybody have solution on that.
       
      Thanks & Regards,
      Nilesh.


      Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now
    • Necdet Çetinkaya
      I think that you can use like this declare @cln varchar(50) declare @cd as int declare @mcd as int declare @sql varchar(100) select @cd=min(cid) from tblx
      Message 2 of 3 , May 8, 2004
      • 0 Attachment

        I think that you can use like this

         

        declare @cln varchar(50)

        declare @cd as int

        declare @mcd as int

        declare @sql varchar(100)

         

        select @cd=min(cid) from tblx

        select @mcd=max(cid) from tblx

        while (@cd)<= @mcd

         begin

          select @cln=name from tblx where cid=@cd

                    set @sql = 'select '+@cln+' from tbly'

                    exec(@sql)

        --  select @cln from tbly

          print @cln

          select @cd=@cd+1

         end

         

        Necdet Çetinkaya

        Genotıp Hasta Bilgi Sistemleri

        necdet@...

         

        -----Original Message-----
        From: nilesh babar [mailto:nilesh_babar2003@...]
        Sent:
        Friday, May 07, 2004 4:11 PM
        To: SQLQueriesNoCode@yahoogroups.com
        Subject: [SQLQueriesNoCode] Query in MS SQL Server

         

        Hi Friends,

        I have one query, im using MS SQL Server 2000.

         

        I have tewo tables tblx and tbly, in tblx two columns cid and name. in tbly what are the column names are available those column names are in tblx name field. 

        In tbly table there are some records, i want these records but only one column at a time. For that picking one columns details from table i have created one variable, so what value in that variable (meanse column name) i want to select from tbly. 

        For that i have using following query but i didn't get output as per my requirement,so please help me.

        Table Details

        tblx

                cid            name

                 1              BASIC

                 2              HRA

                 3              MONTHLY

        tbly

                cid     employeeno   BASIC    HRA    MONTHLY

                  1          1202            5000       2000   7000

                  2          1658            6000       3000   9000

                  3          0562            9000       5000   14000

         

        query

         

        declare @cln varchar(50)
        declare @cd as int
        declare @mcd as int

        select @cd=min(cid) from tblx
        select @mcd=max(cid) from tblx

        while (@cd)<= @mcd
         begin
          select @cln=name from tblx where cid=@cd

          select @cln from tbly
          print @cln
          select @cd=@cd+1
         end

         

         

        Please reply me if anybody have solution on that.

         

        Thanks & Regards,

        Nilesh.


        Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now

      • nilesh babar
        Hi Necdet_Çetinkaya ! Thank you very much, you solved my problem. Thanks for assist to my query. Thanks once again, Regards, Nilesh. Necdet_Çetinkaya
        Message 3 of 3 , May 9, 2004
        • 0 Attachment
          Hi Necdet_Çetinkaya !
          Thank you very much, you solved my problem. Thanks for assist to my query.
           
           Thanks once again,
          Regards,
          Nilesh.

          Necdet_Çetinkaya <necdet@...> wrote:

          I think that you can use like this

           

          declare @cln varchar(50)

          declare @cd as int

          declare @mcd as int

          declare @sql varchar(100)

           

          select @cd=min(cid) from tblx

          select @mcd=max(cid) from tblx

          while (@cd)<= @mcd

           begin

            select @cln=name from tblx where cid=@cd

                      set @sql = 'select '+@cln+' from tbly'

                      exec(@sql)

          --  select @cln from tbly

            print @cln

            select @cd=@cd+1

           end

           

          Necdet Çetinkaya

          Genotýp Hasta Bilgi Sistemleri

          necdet@...

           

          -----Original Message-----
          From: nilesh babar [mailto:nilesh_babar2003@...]
          Sent:
          Friday, May 07, 2004 4:11 PM
          To: SQLQueriesNoCode@yahoogroups.com
          Subject: [SQLQueriesNoCode] Query in MS SQL Server

           

          Hi Friends,

          I have one query, im using MS SQL Server 2000.

           

          I have tewo tables tblx and tbly, in tblx two columns cid and name. in tbly what are the column names are available those column names are in tblx name field. 

          In tbly table there are some records, i want these records but only one column at a time. For that picking one columns details from table i have created one variable, so what value in that variable (meanse column name) i want to select from tbly. 

          For that i have using following query but i didn't get output as per my requirement,so please help me.

          Table Details

          tblx

                  cid            name

                   1              BASIC

                   2              HRA

                   3              MONTHLY

          tbly

                  cid     employeeno   BASIC    HRA    MONTHLY

                    1          1202            5000       2000   7000

                    2          1658            6000       3000   9000

                    3          0562            9000       5000   14000

           

          query

           

          declare @cln varchar(50)
          declare @cd as int
          declare @mcd as int

          select @cd=min(cid) from tblx
          select @mcd=max(cid) from tblx

          while (@cd)<= @mcd
           begin
            select @cln=name from tblx where cid=@cd

            select @cln from tbly
            print @cln
            select @cd=@cd+1
           end

           

           

          Please reply me if anybody have solution on that.

           

          Thanks & Regards,

          Nilesh.


          Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now


          Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now

        Your message has been successfully submitted and would be delivered to recipients shortly.