Solved

problem creating function

Posted on 2014-02-05
10
207 Views
Last Modified: 2014-02-05
Hello,
I want to create a very "simple" function, but I'm getting an error message:
La variable de table "@TABLE_NAME" doit être déclarée.

in english : the table data "TABLE_NAME" must be declare.

I don't understand, this data is declared in the fonction arguments ...

any idea ??

best regards.

CREATE FUNCTION FN_GET_LIBELLE (@TABLE_NAME VARCHAR(50),@PK_COLUMN_NAME VARCHAR(50),@PK_DECIMAL_VALUE INT,      @PK_STRING_VALUE VARCHAR(50))
RETURNS VARCHAR(100)
AS
BEGIN
      -- Declare the return variable here
      DECLARE @LIBELLE VARCHAR(100)
      
      IF (@PK_DECIMAL_VALUE IS NOT NULL and @PK_DECIMAL_VALUE > 0 )
      BEGIN
            SELECT @LIBELLE = LIBELLE FROM @TABLE_NAME WHERE @PK_COLUMN_NAME = @PK_DECIMAL_VALUE
      END
      ELSE
      IF (@PK_STRING_VALUE IS NOT NULL AND @PK_STRING_VALUE NOT LIKE '')
      BEGIN
            SELECT @LIBELLE = LIBELLE FROM @TABLE_NAME WHERE @PK_COLUMN_NAME = ''+@PK_STRING_VALUE+''
      END
      
      RETURN @LIBELLE

END
GO
0
Comment
Question by:bruno_boccara
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 39836003
Seems like you are passing the table name, in that case you need to use dynamic sql , but unfortunately you SQL functions wont support dynamic sql, you need to change this to a stored procedure
0
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 250 total points
ID: 39836004
You cannot use a table name as a variable.

Also, consider changing this:
 IF (@PK_DECIMAL_VALUE IS NOT NULL and @PK_DECIMAL_VALUE > 0 )

to

 IF (isnull(@PK_DECIMAL_VALUE, 0) > 0)

Similarly:

IF (@PK_STRING_VALUE IS NOT NULL AND @PK_STRING_VALUE NOT LIKE '')

to

IF (isnull(@PK_STRING_VALUE, '') <> '')

Don't use NOT LIKE without the wildchar % as it won't work.


If you try and use dynamic SQL to build your query as a string you'll find that you won't be able to properly return the value as it will not be in scope.

What are you hoping to achieve?
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39836006
You can't use a variable as a table name in this fashion.  You would need to use dynamic sql and form the sql query as a string before execution.  Unfortunately dynamic sql can't be used inside a function since they are intended to be deterministic.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39836009
Ahh, yes I forgot you can't use dynamic SQL in a function either.
0
 

Author Comment

by:bruno_boccara
ID: 39836015
that's my principal problem, I can't use EXEC in a function.

so what should I do to go around this problem ?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:bruno_boccara
ID: 39836023
I need a function, not a procedure ....
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39836076
You are coming up against the limitations of the product I'm afraid. You need dynamic SQL which you can't do in a function. As there isn't any other way of doing the dynamic SQL, then you must use a stored procedure.

Why does this need to be a function? I am assuming it is part of another select statement and you're trying to retrieve an arbitrary value, from an arbitrary column in an arbitrary table.
0
 

Author Comment

by:bruno_boccara
ID: 39836093
yes it's something like that....

anyway thanks for your help
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39836098
One alternative is using an extended stored procedure from within your function...

http://technet.microsoft.com/en-us/library/aa214418(v=sql.80).aspx
0
 

Author Comment

by:bruno_boccara
ID: 39836117
I'm on MSSQL 2008
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

760 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

20 Experts available now in Live!

Get 1:1 Help Now