|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to loop through a tableI have the following table X create table x ( a int ) ; insert x values(1); insert x values(2); insert x values(3); Is there a way to extract each a in table X to get the following print result? x is 1 x is 2 x is 3 Thanks a lot!! Michael
Show quote
Hide quote
"Michael" <michae***@gmail.com> wrote in message Replied in .programmingnews:1176324884.807557.48050@n76g2000hsh.googlegroups.com... > Hi, > > I have the following table X > > create table x > ( > a int > ) > ; > > insert x > values(1); > insert x > values(2); > insert x > values(3); > > Is there a way to extract each a in table X to get the following print > result? > > x is 1 > x is 2 > x is 3 > > > Thanks a lot!! > Michael > Please do not multi-post. -- David Portas, SQL Server MVP Whenever possible please post enough code to reproduce your problem. Including CREATE TABLE and INSERT statements usually helps. State what version of SQL Server you are using and specify the content of any error messages. SQL Server Books Online: http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx --
Show quote
Hide quote
"Michael" <michae***@gmail.com> wrote in message There is, but why would you want to loop?news:1176324884.807557.48050@n76g2000hsh.googlegroups.com... > Hi, > > I have the following table X > > create table x > ( > a int > ) > ; > > insert x > values(1); > insert x > values(2); > insert x > values(3); > > Is there a way to extract each a in table X to get the following print > result? Simply do a select. select 'x is ' + cast(a as char(2)) from x; Show quoteHide quote > > x is 1 > x is 2 > x is 3 > > > Thanks a lot!! > Michael > -- Greg Moore SQL Server DBA Consulting Remote and Onsite available! Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
Show quote
Hide quote
On Apr 11, 4:54 pm, "Michael" <michae***@gmail.com> wrote: select 'x is ' + convert(varchar(20),a)> Hi, > > I have the following table X > > create table x > ( > a int > ) > ; > > insert x > values(1); > insert x > values(2); > insert x > values(3); > > Is there a way to extract each a in table X to get the following print > result? > > x is 1 > x is 2 > x is 3 > > Thanks a lot!! > Michael from x order by a Denis the SQL Menace http://sqlservercode.blogspot.com/
Show quote
Hide quote
On Apr 11, 7:57 pm, "SQL Menace" <denis.g***@gmail.com> wrote: Thanks a lot!! The reason why I want to loop through the table is that> On Apr 11, 4:54 pm, "Michael" <michae***@gmail.com> wrote: > > > > > > > Hi, > > > I have the following table X > > > create table x > > ( > > a int > > ) > > ; > > > insert x > > values(1); > > insert x > > values(2); > > insert x > > values(3); > > > Is there a way to extract each a in table X to get the following print > > result? > > > x is 1 > > x is 2 > > x is 3 > > > Thanks a lot!! > > Michael > > select 'x is ' + convert(varchar(20),a) > from x > order by a > > Denis the SQL Menacehttp://sqlservercode.blogspot.com/- Hide quoted text - > > - Show quoted text - I simplified the problem. I need to loop through the table to get exactly what I want. Anyone could show me that? Thanks!! Michael On 12 Apr, 14:06, "Michael" <michae***@gmail.com> wrote: That's an unwise assumption. In my experience it's untrue at least> > Thanks a lot!! The reason why I want to loop through the table is that > I simplified the problem. I need to loop through the table to get > exactly what I want. Anyone could show me that? > > Thanks!! > Michael > 99.99% of the time so do not assume that you will need to loop through the table row by row unless you have had an expert opinion to that effect. Maybe you consider you are an expert but the fact that you are asking this question at all suggests that you should take a much more cautious approach - for example you could post an accurate description of the problem to see if anyone can suggest alternative solutions. What you are asking for is called a cursor. Look up DECLARE CURSOR in Books Online. Cursors can be very bad news in the wrong hands and the stock advice is that you should avoid cursors and write set-based code instead wherever possible. Hope this helps. -- David Portas, SQL Server MVP Whenever possible please post enough code to reproduce your problem. Including CREATE TABLE and INSERT statements usually helps. State what version of SQL Server you are using and specify the content of any error messages. SQL Server Books Online: http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx --
Show quote
Hide quote
"Michael" <michae***@gmail.com> wrote in message Perhaps you should post exactly what you want then. We can only answer the news:1176383189.058521.159980@e65g2000hsc.googlegroups.com... > On Apr 11, 7:57 pm, "SQL Menace" <denis.g***@gmail.com> wrote: >> On Apr 11, 4:54 pm, "Michael" <michae***@gmail.com> wrote: >> >> >> >> >> >> > Hi, >> >> > I have the following table X >> >> > create table x >> > ( >> > a int >> > ) >> > ; >> >> > insert x >> > values(1); >> > insert x >> > values(2); >> > insert x >> > values(3); >> >> > Is there a way to extract each a in table X to get the following print >> > result? >> >> > x is 1 >> > x is 2 >> > x is 3 >> >> > Thanks a lot!! >> > Michael >> >> select 'x is ' + convert(varchar(20),a) >> from x >> order by a >> >> Denis the SQL Menacehttp://sqlservercode.blogspot.com/- Hide quoted >> text - >> >> - Show quoted text - > > Thanks a lot!! The reason why I want to loop through the table is that > I simplified the problem. I need to loop through the table to get > exactly what I want. Anyone could show me that? questions posted. GENERALLY, looping through a table should be and can be avoided at all costs. But, take a look at CURSORS in the Books Online in SQL Server. > > Thanks!! > Michael > -- Greg Moore SQL Server DBA Consulting Remote and Onsite available! Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
Deleted default database, now can't get in
SQL Server 2005 SP2 Backup Errors??? Finding duplicate based on records in same table Select takes 5 minutes, With linked table takes 35 minutes. table size large varchar update fails on sql 2000 server, works on dev. machine LIKE query slower in SQL2005 than in SQL2000 d99_tmp Usedspace size for each table in a database Deleteing SQL Aent Jobs in SQL2005 |
|||||||||||||||||||||||