April 23, 2009 at 10:00 am
I have scenario to update a col A, with col B.
If col A has a record like '%A' and col B is blank, then update col B with col A and delete the record in col A.
I would even like to place the query a function....can anyone help?
April 23, 2009 at 10:17 am
b_boy (4/23/2009)
I have scenario to update a col A, with col B.If col A has a record like '%A' and col B is blank, then update col B with col A and delete the record in col A.
I would even like to place the query a function....can anyone help?
How about a simple update, not in a function? (Untested code follows)
update TABLEA
set colB = colA,
colA = ''
where colA like '%A'
and (colB is null or colB = '')
If this doesn't suffice, please read the link in my signature and post relevant DDL/DML code so that we can assist you better.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 23, 2009 at 10:25 am
Hope the following code will address your requirement if your table has a primary key.
DECLARE @Pkey int
SELECT @PKey = Table_Primary_Key FROM Your_Table_Name
WHERE COLA LIKE '%A' AND COLB IS NULL
UPDATE Your_Table_Name SET COLB = COLA,
COLA = NULL
WHERE Table_Primary_Key = @PKey
Table_Primary_Key - replace with primary column of your table.
Your_Table_Name - replace with your table name.
April 25, 2009 at 3:05 am
You can't modify data in base tables from a user defined function.
If you want to reuse this TSQL you could consider putting it in a stored procedure.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy