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" <anonymous@discussions.microsoft.com> wrote in message
news:BBD660E6-2769-4748-B868-BAB2484DD201@microsoft.com...
> 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 null
> 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,
> lan
>
> ----- Zach Wells wrote: -----
>
>
>
> shawn wrote:
> > 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.
> >
>
> 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.
>
> Zach
>