Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to write this query?

How to write this query?

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    varshavmane
    wrote on last edited by
    #1

    Hello all, I have a table in which I have added new one column and now I want to add data to it. What I want is that column starting values should be from some number and end with the number of rows. Like if I have 1000 records in by table then I want this column to have values from 2200 to 2200 + 1000. I hope I made myself clear. Please help me. Thanks in advance.

    B 1 Reply Last reply
    0
    • V varshavmane

      Hello all, I have a table in which I have added new one column and now I want to add data to it. What I want is that column starting values should be from some number and end with the number of rows. Like if I have 1000 records in by table then I want this column to have values from 2200 to 2200 + 1000. I hope I made myself clear. Please help me. Thanks in advance.

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      declare @newValue as int set @newValue = (selex max(colname) from tablename) insert into tablename ColumnName values (@newValue+1)


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      V 1 Reply Last reply
      0
      • B Blue_Boy

        declare @newValue as int set @newValue = (selex max(colname) from tablename) insert into tablename ColumnName values (@newValue+1)


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        V Offline
        V Offline
        varshavmane
        wrote on last edited by
        #3

        Thanks for the reply but I dont want to insert the records. I want to update one columns values which null now. How can we do this??

        B 1 Reply Last reply
        0
        • V varshavmane

          Thanks for the reply but I dont want to insert the records. I want to update one columns values which null now. How can we do this??

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          I don't understand good ur questions. To update columns which have null value then simple use Update tablename where colname=null and coname=anothercondition If this is again not help then describe in detail your case.


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          V 1 Reply Last reply
          0
          • B Blue_Boy

            I don't understand good ur questions. To update columns which have null value then simple use Update tablename where colname=null and coname=anothercondition If this is again not help then describe in detail your case.


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            V Offline
            V Offline
            varshavmane
            wrote on last edited by
            #5

            Well what I want is: Table Name: tb_Data Columns: Name1, Name2, ID Number of Records : 1000 Something like this:

            Name1 Name2 ID

            N1 N2 null
            N3 N4 null
            N3 N4 null
            N3 N4 null
            Like wise....
            N3 N4 null

            Now ID Column has null values and in ID Column I want it to get values starting from 2200 to 3200 that is records should be now as:

            Name1 Name2 ID

            N1 N2 2200
            N3 N4 2201
            N3 N4 2202
            N3 N4 2203
            Like wise....
            N3 N4 3200

            Is there anyway to do it by Query as Im having records in lakhs. Thanks again.

            B 1 Reply Last reply
            0
            • V varshavmane

              Well what I want is: Table Name: tb_Data Columns: Name1, Name2, ID Number of Records : 1000 Something like this:

              Name1 Name2 ID

              N1 N2 null
              N3 N4 null
              N3 N4 null
              N3 N4 null
              Like wise....
              N3 N4 null

              Now ID Column has null values and in ID Column I want it to get values starting from 2200 to 3200 that is records should be now as:

              Name1 Name2 ID

              N1 N2 2200
              N3 N4 2201
              N3 N4 2202
              N3 N4 2203
              Like wise....
              N3 N4 3200

              Is there anyway to do it by Query as Im having records in lakhs. Thanks again.

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              hi, Well based on your last post I have more clear your situation. It seems that you dont have increament column in table and i make fast solution,maybe is not the best solution but it will work. here is code down below,just copy-modify-paste and use in your real data create table #TempTable ( IdentValue int IDENTITY(1,1), Name1 varchar(255), Name2 varchar(255), ID Int ) insert into #TempTable select name1,name2,id from tabela declare @i as int set @i=0 declare @startValue as int set @startValue =2200 declare @endValue as int set @endValue=3200 while @i<(select count(*) from #temptable) begin set @i=@i+1 set @startValue=@startValue+1 update #tempTable set ID=@startValue where identValue=@i end delete from tabela insert into tabela select name1,name2,id from #temptable drop table #temptable select * from tabela


              I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups