Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Dynamic like

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-02-18 : 09:29:34
Marilen writes "

i have these table

ModelTable :
Code Description
1FMRE11W CLUBWAGON 4X2
1FMRU15W EXPEDITION 4X2
1FMZU77E EXPLORER 4X4





VehicleTable :

Vin Model
1FMRU15W01LA49512
1FMRU15W21LA64339
1FMRU15W81LA64331
1FMRE11W91HB74164
1FMRE11W41HB74153
1FMZU77E01UC60281
1FMZU77E02LA32865




I want to update the model column of VehicleTable by just reading to the modeltable.



Example if vin like '1FMRE11W%' then update modeltable with CLUBWAGON 4X2
.
.
.
vin like '1FMRU15W%' then EXPEDITION 4X2






here is my code :




declare @ssql varchar(800),@code varchar(300)
declare @ctr integer
set @ctr=0
while @ctr < (select count (code) from model)

begin
set @ctr=@ctr+1
set @ssql = 'update vehicletable set model = (SELECT TOP 1 description FROM modeltable WHERE code in (SELECT TOP ' + convert (varchar(3),@ctr) +
' code FROM modeltable ORDER BY code DESC) ORDER BY code ASC) where vin like '

set @code = ' SELECT TOP 1 code FROM modeltable WHERE code in (SELECT TOP ' + convert (varchar(3),@ctr) + ' code FROM modeltable ORDER BY code DESC) ORDER BY code ASC'

set @ssql = @ssql + @code + '%'


exec(@ssql)
end


im getting an syntax error when I want to put '%' at the end of my like statement ... I want it to be dynamic...
pls. help



"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-02-18 : 10:25:13
How about this:

UPDATE V SET V.Model=M.Description
FROM VehicleTable V INNER JOIN ModelTable M ON (LEFT(V.Vin,8)=M.Code)
WHERE V.Model Is Null


You can take out the WHERE clause if you want all the models updated.

Go to Top of Page

marileng
Starting Member

28 Posts

Posted - 2002-02-19 : 19:52:26
how about if the length of the code on modeltable varies?

Go to Top of Page

Tigger
Yak Posting Veteran

85 Posts

Posted - 2002-02-19 : 19:58:33
Try this


Update VehicleTable
Set V.Model = M.Description
From VehicleTable as v,
ModelTable as m
Where v.vin Like m.code + '%'

Go to Top of Page

marileng
Starting Member

28 Posts

Posted - 2002-02-20 : 02:04:54
thanks

Go to Top of Page

marileng
Starting Member

28 Posts

Posted - 2002-02-20 : 02:04:56
thanks

Go to Top of Page
   

- Advertisement -