Solved

What is the difference between a VBA function and a SQL Server function?

Posted on 2009-03-30
6
258 Views
Last Modified: 2013-11-27
What is the difference between a VBA private/public function and a SQL Server function?

I'm jumping from Access to SQL Server and VBA to C# sharp now.
In the past when I had an update query I could do the following SQL -

UPDATE tblColTypes SET tblColTypes.Family = Replace_Symbols([Family])
WITH OWNERACCESS OPTION;

And the Replace_Symbols would be in a VBA module as:

Public Function Replace_Symbols(sVlu As String) As Variant
On Error GoTo Err_This
 
  sVlu = Replace(sVlu, "-", "")
  sVlu = Replace(sVlu, "/", "")
  sVlu = Replace(sVlu, Chr(34), "")
 
  Replace_Symbols = sVlu
 
Exit_This:
    Exit Function
   
Err_This:
    Resume Exit_This
End Function

How would I do this in C# using Visual Studio 2008 - do I create a SQL Server function in under my Server Explorer window?
0
Comment
Question by:stephenlecomptejr
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24023786
yes, a function would do:
create Function dbo.Replace_Symbols(sVlu as varchar(max) ) returns varchar(max)
as 
begin
 set @sVlu = replace(@sVlu, '-', '')
 set @sVlu = replace(@sVlu, '/', '')
 set @sVlu = replace(@sVlu, '''', '')
 reutrn @sVlu 
End 

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24023802
2 notes:

? the functions are created "per database", so make sure you are connected to the correct database when creating the function.
? to use the function in the sql code, you need to prefix with the owner name (dbo.) like in the create:

select dbo.Replace_Symbols('123-456/789''0') data

Open in new window

0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24023806
I would suggest taking a look at LINQ ... since you are making a fresh start

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx


0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 24023869
I get an error - Incorrect syntax near 'sVlu'
Must declare the scalar variable "@sVlu"
Must declare the scalar variable "@sVlu"
Must declare the scalar variable "@sVlu"
ok

I'm doing this inside of Visual Studio 2008 - under Server Explorer - under Database.mdf - Add New Functions...
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24023931
sorry:
create Function dbo.Replace_Symbols(@sVlu as varchar(max) ) returns varchar(max)

as 

begin

 set @sVlu = replace(@sVlu, '-', '')

 set @sVlu = replace(@sVlu, '/', '')

 set @sVlu = replace(@sVlu, '''', '')

 reutrn @sVlu 

End 

Open in new window

0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 31564568
Yea - Thank you sincerely!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now