Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
Problem when using a User Defined Function in...
Problem when using a User Defined Function in a SET loop
Rate Topic
Display Mode
Topic Options
Author
Message
henryvuong1000
henryvuong1000
Posted Sunday, November 18, 2012 9:35 AM
Grasshopper
Group: General Forum Members
Last Login: Saturday, December 22, 2012 4:35 PM
Points: 12,
Visits: 27
I am using SQL Server 2008 R2. I created a User Defined Function like this:
CREATE FUNCTION [dbo].[Custom_StringToTableWithID]
(
@string VARCHAR(MAX),
@delimiter CHAR(1)
)
--The return table has a column with auto-increment primary key and a column with text
--The text column is the result of the split string from the input
RETURNS @output TABLE( ID int identity primary key, Data VARCHAR(MAX))
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (data)
--Stripped off any new line character, carriage return character, leading and trailing spaces in the insert value
--Each new line and carrage return characters is replaced by a blank space
VALUES (LTRIM(RTRIM(REPLACE(REPLACE(SUBSTRING(@string, @start, @end - @start),CHAR(10), ' '), CHAR(13), ' '))))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
I have a table named "CUSTOM_test" with two columns:
ID Title
Item1 Lord of the Rings
Item2 The Hobbits
Item3 Dark Knight Rises
When I write code like this, the value of @word is "Lord":
DECLARE @title nvarchar(100)
SET @title = (SELECT Title FROM CUSTOM_test WHERE ID = 'Item1')
DECLARE @word nvarchar(20)
SET @word = (SELECT Data FROM Custom_StringToTableWithID(@title, ' ') WHERE ID = 1)
But when I write code like this, the value of @word is NULL:
DECLARE @title nvarchar(100)
DECLARE @word nvarchar(20)
UPDATE CUSTOM_test
SET
@title = Title,
@word = (SELECT Data FROM Custom_StringToTableWithID(@title, ' ') WHERE ID = 1)
WHERE ID = 'Item1'
The later code is just a simplified version. I actually need to loop through the whole table and there's more code in that, but it cannot work as long as @word is null. Can someone give me an explanation why @word is null? Thanks.
.
Post #1386056
Jeff Moden
Jeff Moden
Posted Sunday, November 18, 2012 11:48 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
For starters, your UPDATE code isn't updating anything in the table.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1386081
Vedran Kesegic
Vedran Kesegic
Posted Monday, November 19, 2012 12:22 AM
Old Hand
Group: General Forum Members
Last Login: Yesterday @ 12:57 PM
Points: 343,
Visits: 1,089
Your @title is null (not set), hence function is returning NULL. Why quirky update? It is probably the last thing I would do.
_____________________________________________________
XDetails Addin
- for SQL Developers and DBA
blog.sqlxdetails.com
- Transaction log myths - debunked!
Post #1386179
Jeff Moden
Jeff Moden
Posted Monday, November 19, 2012 6:36 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
Vedran Kesegic (11/19/2012)
Your @title is null (not set), hence function is returning NULL. Why quirky update? It is probably the last thing I would do.
Look again... it's not a quirky update because the code doesn't actually update any data.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1386357
Vedran Kesegic
Vedran Kesegic
Posted Monday, November 19, 2012 6:38 AM
Old Hand
Group: General Forum Members
Last Login: Yesterday @ 12:57 PM
Points: 343,
Visits: 1,089
It updates (sets) variables to null.
_____________________________________________________
XDetails Addin
- for SQL Developers and DBA
blog.sqlxdetails.com
- Transaction log myths - debunked!
Post #1386361
Jeff Moden
Jeff Moden
Posted Monday, November 19, 2012 6:45 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
Vedran Kesegic (11/19/2012)
It updates (sets) variables to null.
Understood but it's still not a quirky update. Not even close to being one.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1386364
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.