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 2005 Forums
 Transact-SQL (2005)
 Converting code from VBA to T-SQL

Author  Topic 

Trudye
Posting Yak Master

169 Posts

Posted - 2009-01-14 : 13:04:03
Hi Guys, I have to convert the following code from VBA to SQL. My first question is can it be done in SQL? Do I have to write a UDF or can it be done in T-SQL?

cnt = 0
H_comment = G_comment
h_len1 = Len(H_comment)
x = 1
doIt = h_len1 / 50
loopIt = doIt
doIt = h_len1 Mod 50

If doIt > 0 Then
loopIt = loopIt + 1
End If

Do While loopIt > 0
S_comment = Mid(H_comment, x, 50)
If S_comment = "" Then
Else
cnt = cnt + 1
GoSub write_Comment
End If
loopIt = loopIt - 1
x = x + 50
Loop

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-01-15 : 06:37:43
In answer to yuor questions

1) Can this be done in SQL?
YES

2) Do I have to write a UDF or can it be done in T-SQL?
If you need the proccess to DO something you can't write it as a UDF -- you could write it as a stored proc easily.

Here's a *direct* (SQL so horrible that dead database admins are revolving in their graves right now) translation of your vb code.

DECLARE @cnt INT SET @cnt = 0
DECLARE @H_comment NVARCHAR(MAX) SET @H_comment = 'This is a comment more than 50 characters long. This will be split into sets of 50 characters. There is no reason to do this in SQL, not is this a good implementation of such a way to do so. Also @cnt is declared but never used.'
DECLARE @h_len1 INT SET @h_len1 = LEN(@H_comment)
DECLARE @x INT SET @x = 1
DECLARE @doIt FLOAT SET @doIt = @h_len1 % 50
DECLARE @loopIt FLOAT SET @loopIt = @doIt

DECLARE @S_comment NVARCHAR(50) SET @S_comment = NULL

SET @doIt = @h_len1 % 50

IF @doIt > 0 SET @loopIt = @loopIt + 1

WHILE @loopIt > 0 BEGIN

SET @S_comment = SUBSTRING(@H_comment, @x, 50)

IF @S_comment > '' BEGIN
SET @cnt = @cnt + 1
-- Do write_comment here (this could be a proc)
PRINT @S_comment
END

SET @loopIt = @loopIt - 1
SET @x = @x + 50
END


Results =

This is a comment more than 50 characters long. Th
is will be split into sets of 50 characters. There
is no reason to do this in SQL, not is this a goo
d implementation of such a way to do so. Also @cnt
is declared but never used.

-- Now for a question you didn't ask

3) Should I be using SQL for this?
NO

==============================

If you have to split a string into chunks like this in SQL then maybe you should think of a table valued function -- there are a lot of posts on string splitting functions on this site.




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-15 : 06:59:43
Isn't this easier?

DECLARE @H_comment NVARCHAR(4000)

SET @H_comment = 'This is a comment more than 50 characters long. This will be split into sets of 50 characters. There is no reason to do this in SQL, not is this a good implementation of such a way to do so. Also @cnt is declared but never used.'

SELECT @H_comment,
1 + number,
SUBSTRING(@H_comment, 1 + 50 * Number, 50)
FROM master..spt_values
WHERE Type = 'P'
AND LEN(@H_comment) / 50 >= Number



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-01-15 : 07:20:20
Yes -- of course that's easier. So would a table valued function. Pretty much anything would have been easier.

I just gave him exactly what he asked for.........



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-15 : 09:05:13
Me too!


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2009-01-16 : 12:29:33
Thanks to everyone for the GREAT response. I took something from each of you and got the job done. You Guys are life savers.

BE well and have a greate weekend,
Trudye
Go to Top of Page
   

- Advertisement -