June 24, 2009 at 1:05 pm
Hi,
I have a stored procedure that is bringing code over form a legacy system. That system had a column called 'type' defined as a varchar and contained data like 'Computer' or 'Class'.
I have written a function, fnType, that takes in that varchar value and returns an integer corresponding to the type on our current system. The value in I am passing to the function is from a cursor, if that matters, it too is defined as a varchar.
I'd like to use the udf returned value in my insert statement like:
Insert into newtable (trainingType, otherCols) values (fnType(@varcharType), OtherValues)
or
Insert into newtable (trainingType, otherCols ) values (select fnType(@varcharType), OtherValues)
Could someone help me with the proper syntax to use in this case? I've not been able to find a solution via google or by searching this form/site.
Thank you!
Even as a mother protects with her life
Her child, her only child,
So with a boundless heart
Should one cherish all living beings;
June 24, 2009 at 1:09 pm
functions almost always need to be preceded with the owner/schema
so either of these should work:
Insert into newtable (trainingType, otherCols)
values (dbo.fnType(@varcharType), OtherValues)
or
Insert into newtable (trainingType, otherCols)
select dbo.fnType(@varcharType), OtherValues
Lowell
June 24, 2009 at 1:16 pm
It's that easy? Wow...I actually guessed right...lol..that doesn't happen very often 🙂
Thanks!
Even as a mother protects with her life
Her child, her only child,
So with a boundless heart
Should one cherish all living beings;
June 24, 2009 at 2:10 pm
Whilst I won't pretend that I know your system better than you, might I suggest you change the way that you are doing this?
If you put your varchar values into a table, with your corresponding integer value, then you could easily run a joined insert statement.
Something like this:
declare @Tmp table (
[Type] varchar(10),
TypeNbr int)
insert into @Tmp
select 'Computer', 1 UNION ALL
select 'Class', 2
insert into newtable (trainingType, otherCols)
select @Tmp.typeNbr, otherCols
from @Tmp
where [Type] = @varcharType
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 25, 2009 at 6:26 am
Actually this is a script we're using to load data from a legacy system into our current system and don't need to store the old varchars. Once we do the import the script and those damned varchars will be gone forever...I hope.:hehe:
Even as a mother protects with her life
Her child, her only child,
So with a boundless heart
Should one cherish all living beings;
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply