Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


cursor appropriate?


cursor appropriate?

Author
Message
BeginnerBug
BeginnerBug
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 350
is cursor needed for following scenario?

create table #table1 (col1 int,col2 varchar(30))
insert into #table1 values (1,'martin')
insert into #table1 values (2,'tom')
insert into #table1 values (3,'schrof')

create table #table2(t_col1 int identity(1,1),t_col2 int)

create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int)

1.i have to select the values from table1 and insert the value of table1.col1 into table2.t_col2

2. fetch the auto increment value and insert into table3.f_col3 and insert the value of table1.col2 into table3.f_col3


cursor is necessary to achieve this?.. any suggestion pls?
tommyh
tommyh
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1598 Visits: 2000
Not needed

Depending on uniqueness something like

insert into #table2 (t_col2) select col1 from #table1
insert into #table3 (f_col3) select t_col1 from #table2 t2 join #table1 t1 on t1.col1 = t2.t_col2


could work.

If one would actually need to insert the values one at a time i would use a while instead. Like

declare @i int
set @i = 0

while exists (select * from #table1 where col1 > @i)
begin
select top 1 @i = col1 from #table1 where col1 > @i
insert into #table2 (t_col2) values (@i)
insert into #table3 (f_col3) values(SCOPE_IDENTITY())
end



/T
BeginnerBug
BeginnerBug
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 350
Great thanks tommy
ChrisM@Work
ChrisM@Work
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10136 Visits: 19211
MonsterRocks (11/22/2010)
is cursor needed for following scenario?

create table #table1 (col1 int,col2 varchar(30))
insert into #table1 values (1,'martin')
insert into #table1 values (2,'tom')
insert into #table1 values (3,'schrof')

create table #table2(t_col1 int identity(1,1),t_col2 int)

create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int)

1.i have to select the values from table1 and insert the value of table1.col1 into table2.t_col2

2. fetch the auto increment value and insert into table3.f_col3 and insert the value of table1.col2 into table3.f_col3


cursor is necessary to achieve this?.. any suggestion pls?


You don't need a cursor or a WHILE loop, SQL Server has the OUTPUT clause for this purpose. See BOL, the examples are excellent.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
BeginnerBug
BeginnerBug
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 350
create table #table1 (col1 int,col2 varchar(30))
insert into #table1 values (1,'martin')
insert into #table1 values (2,'tom')
insert into #table1 values (3,'schrof')

create table #table2(t_col1 int identity(1,1),t_col2 int)

create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int)




insert into #table2 (t_col2) output inserted.t_col1,t.col1 into #table3(f_col2,f_col3) select col1 from #table1 t




i am getting folowing error

The multi-part identifier "t.col1" could not be bound.


what could be the problem?...any suggestion pls
ChrisM@Work
ChrisM@Work
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10136 Visits: 19211
MonsterRocks (11/22/2010)
create table #table1 (col1 int,col2 varchar(30))
insert into #table1 values (1,'martin')
insert into #table1 values (2,'tom')
insert into #table1 values (3,'schrof')

create table #table2(t_col1 int identity(1,1),t_col2 int)

create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int)




insert into #table2 (t_col2) output inserted.t_col1,t.col1 into #table3(f_col2,f_col3) select col1 from #table1 t







i am getting folowing error

The multi-part identifier "t.col1" could not be bound.


what could be the problem?...any suggestion pls


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
BeginnerBug
BeginnerBug
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 350
Chris.. the links in ur signature u want me to check out?
WayneS
WayneS
SSCertifiable
SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)SSCertifiable (6.9K reputation)

Group: General Forum Members
Points: 6867 Visits: 10429
Try this:
if object_id('tempdb..#table1') IS NOT NULL DROP TABLE #table1;
if object_id('tempdb..#table2') IS NOT NULL DROP TABLE #table2;
if object_id('tempdb..#table3') IS NOT NULL DROP TABLE #table3;

create table #table1 (col1 int,col2 varchar(30));
insert into #table1 values (1,'martin');
insert into #table1 values (2,'tom');
insert into #table1 values (3,'schrof');

create table #table2(t_col1 int identity(1,1),t_col2 int);
create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int);

insert into #table2 (t_col2) output inserted.t_col1, inserted.t_col2 into #table3(f_col2,f_col3)
select col1 from #table1 t;



Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6231 Visits: 7660
Removed, inaccurate.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
ChrisM@home
ChrisM@home
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1275 Visits: 9863
MonsterRocks (11/22/2010)
Chris.. the links in ur signature u want me to check out?


@MonsterRocks - Sorry mate, got tied up, but Wayne jumped in with solution.
@Wayne - cheers mate, sorted.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search