Groups | Blog | Home
all groups > sql server programming > may 2004 >

sql server programming : store procedure or ...


shawn
5/7/2004 7:01:05 PM
Can I create a store procedure that I could fire up from a Visual Basic program?
If so, how do I create the procedure, which could update tableA.col1 and tableA.col2 from tableA based on the join key colkey in tableB with tableB.col1 and tableB.col2.
lanx
5/7/2004 7:36:04 PM
Hi Zach
So how do you exactly calling a S.P. from VB

Here are currently how I running a couple of steps to get it done in VB, which I don't think it is an effective way -
1) select colkey from tableA where col1 is null and col2 is nul
then get the result and put in loop, for each colkey put in a variable
2) select col1, col2 from tableB where colkey=variablecolkey
then get the result an put into variables
3) update tableA set col1=variablecol1, col2=variablecol2 where colkey=variablecolkey
4) continue the loop

Can you show me the sample how would you write the S.P. for the same result

Thank you
la

----- Zach Wells wrote: ----



[quoted text, click to view]

Yes, you can

You create the procedure using the "create procedure" command (see books
online for exact syntax

As far as how to update a table, quite frankly, given the minimal
information you posted, I would do you a disservice by trying to post an
update statement. Using update incorrectly can over-write every row of
data you have in an instant and given your obvious newness, you wouldn't
know any better until it was too late

If you'd like to post the DDL (DDL is the data definition language,
defines your tables) and some sample data, we might be able to help you
a bit more

Zac
Shawn
5/7/2004 8:46:12 PM
I found the way calling a S.P. from VB, but not sure how do I write that S.P

Any input will be welcome


----- lanx wrote: ----

Hi Zach
So how do you exactly calling a S.P. from VB

Here are currently how I running a couple of steps to get it done in VB, which I don't think it is an effective way -
1) select colkey from tableA where col1 is null and col2 is nul
then get the result and put in loop, for each colkey put in a variable
2) select col1, col2 from tableB where colkey=variablecolkey
then get the result an put into variables
3) update tableA set col1=variablecol1, col2=variablecol2 where colkey=variablecolkey
4) continue the loop

Can you show me the sample how would you write the S.P. for the same result


Thank you
la

----- Zach Wells wrote: ----



[quoted text, click to view]

Yes, you can

You create the procedure using the "create procedure" command (see books
online for exact syntax

As far as how to update a table, quite frankly, given the minimal
information you posted, I would do you a disservice by trying to post an
update statement. Using update incorrectly can over-write every row of
data you have in an instant and given your obvious newness, you wouldn't
know any better until it was too late

If you'd like to post the DDL (DDL is the data definition language,
defines your tables) and some sample data, we might be able to help you
a bit more

Zac
Zach Wells
5/7/2004 10:15:47 PM


[quoted text, click to view]

Yes, you can.

You create the procedure using the "create procedure" command (see books
online for exact syntax)

As far as how to update a table, quite frankly, given the minimal
information you posted, I would do you a disservice by trying to post an
update statement. Using update incorrectly can over-write every row of
data you have in an instant and given your obvious newness, you wouldn't
know any better until it was too late.

If you'd like to post the DDL (DDL is the data definition language,
defines your tables) and some sample data, we might be able to help you
a bit more.

Dan Guzman
5/7/2004 11:46:16 PM
[quoted text, click to view]

I'm glad you understand this is not a good method. It is usually best to
avoid procedural processes like looping and cursors with a relational
database. Whenever possible, let SQL Server perform the task rather than
trying to do all the procedural code yourself.

I believe you'll fund that the set-based approach below will perform *much*
faster. You can execute the following statement directly from your
application or encapsulate it in a stored procedure.

UPDATE a
SET
col1 = b.col1,
col2 = b.col2
FROM tableA a
JOIN tableb b ON
a.colkey = b.colkey
WHERE a.col1 IS NULL AND a.col2 IS NULL

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
tableA.col1 and tableA.col2 from tableA based on the join key colkey in
tableB with tableB.col1 and tableB.col2.
[quoted text, click to view]

Aaron Bertrand [MVP]
5/8/2004 12:14:09 AM
Here's some DDL, sample data and an example query using #temp tables. Run
the query in Query Analyzer and compare the before and after state of each
table.

See the topic CREATE PROCEDURE in Books Online for information on moving the
UPDATE query to a stored procedure (basically wrap it with CREATE PROCEDURE
and eliminate the # signs).





set nocount on

create table #tableA
(
colkey INT primary key,
col1 int null,
col2 int null
)

insert #tableA(colkey) select 1
insert #tableA(colkey) select 2
insert #tableA(colkey) select 3
insert #tableA select 4,0,0 -- a row not null
insert #tableA(colkey) select 6 -- a row not in tableB

create table #tableB
(
colkey INT primary key,
col1 int not null,
col2 int not null
)

insert #tableB select 1,2,3
insert #tableB select 2,3,4
insert #tableB select 3,4,5
insert #tableB select 4,1,1
insert #tableB select 5,10,10 -- a row not in tableA

select * from #tableA
select * from #tableB

UPDATE #tableA
SET
col1 = tb.col1,
col2 = tb.col2
FROM #tableA ta
INNER JOIN #tableB tb
ON ta.colkey = tb.colkey
WHERE COALESCE(ta.col1, ta.col2) IS NULL

select * from #tableA
select * from #tableB

drop table #tableA
drop table #tableB





--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/


[quoted text, click to view]

lanx
5/8/2004 11:01:03 PM
Hi Aaron and Dan
Thank you so much for the great help. I am getting there.

Have a great SQL time

AddThis Social Bookmark Button