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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. select statement to display name 1000 times

select statement to display name 1000 times

Scheduled Pinned Locked Moved Database
databasehelptutorial
16 Posts 6 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.
  • A arun_pk

    pls help me out to display a string for example 'abcd' 1000 times using select statement in SQL

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

    One of the ways is:

    create table #tblname (
    col1 varchar(200)
    )

    declare @i as int
    set @i=0
    while @i<=1000
    begin
    set @i=@i+1
    insert into #tblname values ('abc')
    end

    select * from #tblname

    drop table #tblname


    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. www.aktualiteti.com

    A 1 Reply Last reply
    0
    • N Niladri_Biswas

      Follow the third approach i.e.

      recursive cte

      :)

      Niladri Biswas

      A Offline
      A Offline
      arun_pk
      wrote on last edited by
      #6

      thanks its coming how will i be able to insert that 'abcd' 1000 times in my insert given below insert into [dbo].[Test](NAME)

      N 1 Reply Last reply
      0
      • B Blue_Boy

        One of the ways is:

        create table #tblname (
        col1 varchar(200)
        )

        declare @i as int
        set @i=0
        while @i<=1000
        begin
        set @i=@i+1
        insert into #tblname values ('abc')
        end

        select * from #tblname

        drop table #tblname


        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. www.aktualiteti.com

        A Offline
        A Offline
        arun_pk
        wrote on last edited by
        #7

        thanks for the response i m looking for doing with out looping

        1 Reply Last reply
        0
        • A arun_pk

          thanks its coming how will i be able to insert that 'abcd' 1000 times in my insert given below insert into [dbo].[Test](NAME)

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #8

          A) With recursive cte approach

          declare @t table(name varchar(50))
          ;with cte as
          ( select 1 as rn,'abcd' as data
          union all
          select rn+1, 'abcd' as data from cte where rn<1000)

          insert into @t
          select data from cte option (maxrecursion 0)

          select * from @t

          B) Xquery approach

          declare @t table(name varchar(50))
          DECLARE @x XML
          SELECT @x = REPLICATE ('<x>abcd</x>', 1000)
          insert into @t
          SELECT i.value('.', 'VARCHAR(MAX)') ReplacedStrings
          FROM @x.nodes('//x') x(i)

          select * from @t

          :)

          Niladri Biswas

          A 1 Reply Last reply
          0
          • A arun_pk

            pls help me out to display a string for example 'abcd' 1000 times using select statement in SQL

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #9

            use the same method as you were told previously, a cartesian join, and limit it

            select top 1000 'a'
            from dbo.sysobjects as so1, dbo.sysobjects as so2

            and buy a book on SQL.

            Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

            A M 2 Replies Last reply
            0
            • N Niladri_Biswas

              A) With recursive cte approach

              declare @t table(name varchar(50))
              ;with cte as
              ( select 1 as rn,'abcd' as data
              union all
              select rn+1, 'abcd' as data from cte where rn<1000)

              insert into @t
              select data from cte option (maxrecursion 0)

              select * from @t

              B) Xquery approach

              declare @t table(name varchar(50))
              DECLARE @x XML
              SELECT @x = REPLICATE ('<x>abcd</x>', 1000)
              insert into @t
              SELECT i.value('.', 'VARCHAR(MAX)') ReplacedStrings
              FROM @x.nodes('//x') x(i)

              select * from @t

              :)

              Niladri Biswas

              A Offline
              A Offline
              arun_pk
              wrote on last edited by
              #10

              thnks a looooooot

              1 Reply Last reply
              0
              • A Ashfield

                use the same method as you were told previously, a cartesian join, and limit it

                select top 1000 'a'
                from dbo.sysobjects as so1, dbo.sysobjects as so2

                and buy a book on SQL.

                Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                A Offline
                A Offline
                arun_pk
                wrote on last edited by
                #11

                thanks for ur advice but u are failed to answer my q u buy SQL basics

                A L 2 Replies Last reply
                0
                • A arun_pk

                  thanks for ur advice but u are failed to answer my q u buy SQL basics

                  A Offline
                  A Offline
                  Ashfield
                  wrote on last edited by
                  #12

                  arun_pk wrote:

                  but u are failed to answer my q

                  In what respect?

                  select top 1000 'a' from dbo.sysobjects as so1, dbo.sysobjects as so2

                  will do as your (vague) question asks.

                  arun_pk wrote:

                  u buy SQL basics

                  Why? Its not me asking basic questions about sql, its you that doesn't understand what you are being told will work.

                  Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                  1 Reply Last reply
                  0
                  • A arun_pk

                    thanks for ur advice but u are failed to answer my q u buy SQL basics

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    arun_pk wrote:

                    but u are failed to answer my q

                    Ashfield's query works perfectly for your requirement.

                    arun_pk wrote:

                    u buy SQL basics

                    I just saw you being spoon fed by Niladri_Biswas and you are advising others to learn SQL basics ? Funny.

                    1 Reply Last reply
                    0
                    • A Ashfield

                      use the same method as you were told previously, a cartesian join, and limit it

                      select top 1000 'a'
                      from dbo.sysobjects as so1, dbo.sysobjects as so2

                      and buy a book on SQL.

                      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #14

                      Cute - what if sysobjects less than 100 records, add another cross join, and another :laugh: !

                      Never underestimate the power of human stupidity RAH

                      A 1 Reply Last reply
                      0
                      • M Mycroft Holmes

                        Cute - what if sysobjects less than 100 records, add another cross join, and another :laugh: !

                        Never underestimate the power of human stupidity RAH

                        A Offline
                        A Offline
                        Ashfield
                        wrote on last edited by
                        #15

                        Mycroft Holmes wrote:

                        what if sysobjects less than 100 records, add another cross join

                        Yep. Mind you, by default sysobjects has about 40 rows, and 40 * 40 is 1600 so he should be safe. And at the end of the day, he now has enough info to work it out for himself :)

                        Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                        M 1 Reply Last reply
                        0
                        • A Ashfield

                          Mycroft Holmes wrote:

                          what if sysobjects less than 100 records, add another cross join

                          Yep. Mind you, by default sysobjects has about 40 rows, and 40 * 40 is 1600 so he should be safe. And at the end of the day, he now has enough info to work it out for himself :)

                          Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                          M Offline
                          M Offline
                          Mycroft Holmes
                          wrote on last edited by
                          #16

                          Ashfield wrote:

                          he now has enough info to work it out for himself

                          Not with the spoon feeding Niladri had to do, I doubt he has the nous to research cross join!

                          Never underestimate the power of human stupidity RAH

                          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