Showing posts with label cursor. Show all posts
Showing posts with label cursor. Show all posts

Friday, March 9, 2012

Newbie CURSOR question, insert values into a table?

How can I insert values into a table based on the fetch I do. The examples
I have been able to find all shows how to use "print" to show how Cursors
work.

I want to read through a table, and only insert values into another table if
some conditions on the row is true.

Can this be done, if so how?

Thanks in advance.

Henrik.You can do this, but in general you should avoid cursors unless there
is no other choice. It may be that your cursor can be written as a
single insert statement, in which case it will be a lot faster and
easier to code. See below for a rough example.

Simon

/* With a cursor */

declare @.var1 int, @.var2 int

declare cur cursor fast_forward for
select col1, col2
from dbo.table1

open cur

fetch cur into @.var1, @.var2

while @.@.fetch_status = 0
begin
if @.var1 > @.var2 -- check conditions
begin
insert into dbo.table2 (col1, col2)
select @.var1, @.var2
end

fetch cur into @.var1, @.var2
end

close cur
deallocate cur

/* With an insert */

insert into dbo.table2 (col1, col2)
select col1, col2
from dbo.table1
where col1 > col2

"Henrik Hjllund Hansen" <hh@.dlf.dk> wrote in message news:<be367e$2fng$1@.news.cybercity.dk>...
> How can I insert values into a table based on the fetch I do. The examples
> I have been able to find all shows how to use "print" to show how Cursors
> work.
> I want to read through a table, and only insert values into another table if
> some conditions on the row is true.
> Can this be done, if so how?
> Thanks in advance.
> Henrik.|||Hi,

Refer solution below:

Entity/Objects:
(Assume all fields in this example are varchar).

Table1 (Field1,Field2)
Table2 (Field1,Field2, Field3)

Script:
Declare @.F1 Varchar, @.F2 Varchar

--Get records
Declare RS cursor for Select Field1, Field2 from Table1 Where Field1=
A%'
Open RS --Open cursor

Fetch Next From RS into @.F1, @.F2
While @.@.Fetch_Status=0 --Check cursor if end of file
Begin
--Your insert in second table if condition matches.
If RTrim(@.F1) = ABC' And RTrim(@.F2) = Google'
Begin
--Post script to insert in Table2
Insert Into Table2

End

Fetch Next From RS into @.F1, @.F2
End

Close RS --Close cursor
Deallocate RS --relaese memory.

For additional information on updating selected columns in cursor and
complete syntax refer SQL Server Books Online sections.
a)DECLARE CURSOR
b)@.@.Fetch_Status

Hope this helps you.
Thanks Amit

"Henrik Hjllund Hansen" <hh@.dlf.dk> wrote in message news:<be367e$2fng$1@.news.cybercity.dk>...
> How can I insert values into a table based on the fetch I do. The examples
> I have been able to find all shows how to use "print" to show how Cursors
> work.
> I want to read through a table, and only insert values into another table if
> some conditions on the row is true.
> Can this be done, if so how?
> Thanks in advance.
> Henrik.|||Thanks a lot for your answers.

Henrik

"Henrik Hjllund Hansen" <hh@.dlf.dk> wrote in message
news:be367e$2fng$1@.news.cybercity.dk...
> How can I insert values into a table based on the fetch I do. The
examples
> I have been able to find all shows how to use "print" to show how Cursors
> work.
> I want to read through a table, and only insert values into another table
if
> some conditions on the row is true.
> Can this be done, if so how?
> Thanks in advance.
> Henrik.

Monday, February 20, 2012

Newbe to SPs

Ey,
I need to do few operations on every record in a table. Do I have to use cursor? Do SQL has something like 'FOR' or 'WHILE'? I've read somewere that cursors should be avoided due to their time consumption.
ThnxTell us what you need to do ... before we can comment on whether you need a cursor or not.

It would be helpful if you paste some DDL ... and sample data|||That was qucik :)
Im trying to write proc that would be run on a daily basis. It would have to deal with around 12000 records. What it has to do is to take data from flat, multicolumn table, check for some conditions and spread them into real relational db.
Lets say people input things into that flat table and they often misspell i.e. city names due to fast input. It gotta take a record, chceck if inserted city name exists in cities table in db, if not it goes to the missspelled names table and chcecks if it exists there, if not again it adds a new record to missspelled table and lights a flag to inform admin and he could make a decision if to move it to cities table or leave it in misspelled.
Same thing would happen for few columns in every row.

I hope I made myself clear enough...

Thnx again.