Avatar billede enlize Nybegynder
18. september 2001 - 19:17 Der er 13 kommentarer og
1 løsning

Funktioner i en stored procedure

Hej

Jeg sidder og prøver at forstå hvordan man laver en funktion i en stored procedure.

Kan det lade sig gøre at lave en funktion der konverterer en streng fra en datatype til en anden også retunerer denne.

F.eks.
Create Function myFunction
  (@Param As Varchar)
As
.... Også allerede her går det galt for mig...

Håber at der er nogen der kan hjælpe.
Avatar billede terry Ekspert
18. september 2001 - 19:35 #1
have you looked in books online under stored procedures
The syntax for creating a PROCEDURE is ... and also the data type must be a valid type, I do not think Varchar is (Access!)

CREATE PROC[EDURE] procedure_name [;number]
    [
        {@parameter data_type} [VARYING] [= default] [OUTPUT]
    ]
    [,...n]
[WITH
    {
        RECOMPILE
        | ENCRYPTION
        | RECOMPILE, ENCRYPTION
    }
]
[FOR REPLICATION]
AS
    sql_statement [...n]


Avatar billede terry Ekspert
18. september 2001 - 19:38 #2
An example

D. Use OUTPUT parameters
OUTPUT parameters allow an external procedure, a batch, or more than one Transact-SQL statements to access a value set during the procedure execution. In this example, a stored procedure (titles_sum) is created and allows one optional input parameter and one output parameter.

First, create the procedure:

USE pubs

GO

IF EXISTS(SELECT name FROM sysobjects

        WHERE name = \'titles_sum\' AND type = \'P\')

    DROP PROCEDURE titles_sum

GO

USE pubs

GO

CREATE PROCEDURE titles_sum @@TITLE varchar(40) = \'%\', @@SUM money OUTPUT

AS

SELECT \'Title Name\' = title

FROM titles

WHERE title LIKE @@TITLE

SELECT @@SUM = SUM(price)

FROM titles

WHERE title LIKE @@TITLE

GO

 
Avatar billede enlize Nybegynder
18. september 2001 - 19:41 #3
Jeg har læst noget af den dokumentation omkring stored procedure. Men det du kommer med her er da ikke funktion men en procedure... eller?
Avatar billede terry Ekspert
18. september 2001 - 19:53 #4
UCREATE FUNCTION fn_MyFunc
Avatar billede terry Ekspert
18. september 2001 - 19:54 #5
That last comment wast finsihed, so forget it!

Avatar billede terry Ekspert
18. september 2001 - 19:57 #6

CREATE FUNCTION fn_Test
(@myInput nvarchar(30))
RETURNS nvarchar(30)
BEGIN
  IF @myInput IS NULL
  SET @myInput = \"NOT Applicable\"
  RETURN @MyInput
END
Avatar billede terry Ekspert
18. september 2001 - 19:59 #7
You would then use the function as follows.

SELECT LastName, City, dbo.fn_Test(Region) AS Region, Country FROM dbo.Employees
Avatar billede enlize Nybegynder
18. september 2001 - 20:00 #8
Fedest mand - tusind tak
Avatar billede terry Ekspert
18. september 2001 - 20:04 #9
your welcome, and thank you!
Avatar billede dfens Nybegynder
18. september 2001 - 20:08 #10
Terry:

I remember someone told me that Funtions are a SQL2000 feature, can you confirm/deny this?

Is it also available in SQL7?
Avatar billede terry Ekspert
18. september 2001 - 20:15 #11
Well I am no expert at MS SQL but as far I as know, user defined functions exist in 7.0 but they have to be in DDL\'s or something. They are very much like stored procedures in 2000.
Avatar billede torbenkoch Nybegynder
18. september 2001 - 20:45 #12
Userdefined functions does not exist in SQL 7.0. You can, though, create socalled extended stored procedures, which are, as you say, implemented in DLL\'s. But they are not called as functions but as stored procedures.

Functions can - in opposition to stored procedures - be called in the middle of e.g. SELECT\'s as in Terry\'s example.
Avatar billede terry Ekspert
18. september 2001 - 21:16 #13
hi torbenkoch> As I said I am not expert at MS SQL but read books online which stated the following, so I assumed they were called USER DEFINED FUNCTIONS: But I think it sthe same we are talking about. Even so, things are much easier in 2000.

User-defined Functions with MDX Syntax
You can create and register your own functions that operate on multidimensional data. These functions, called “user-defined functions” (UDFs), can accept arguments and return values in the MDX syntax. You can create UDFs using Component Object Model (COM) automation languages such as Microsoft® Visual Basic® or Microsoft Visual C++®. A UDF can be developed on any tool capable of generating Microsoft ActiveX® libraries.
Avatar billede torbenkoch Nybegynder
18. september 2001 - 21:20 #14
Ahh, well - yes, here we are talking about the Data Warehousing functionality - which I am no expert in, either ;-)

But still, they don\'t look and function exactly like userdefined functions in SQL 2000. But still a good point!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester