Groups | Blog | Home
all groups > sql server programming > september 2005 >

sql server programming : Openquery syntax for function


Lisa
9/3/2005 8:26:05 PM
There is a very complex query where I'm trying to call a function from a
linked server with input paramaters from the local database. This is the
general idea:

select openquery( linkserver, 'database.dbo.function( t1.c1, t2.c2 )'
from table1 t1
and table t2
where ...

I get a syntax error that doesn't recognize t1 and t2. How should I fix this?

oj
9/3/2005 10:58:16 PM
Openquery() (i.e. ad-hoc/pass through function) only takes literal strings.
So, it's not possible to pass in any parameters.

Also, it's not possible to call a remote user-defined function in sqlserver
(i.e. srv.db.dbo.udf() is not allowed). So, you would have to create the
function locally.

--
-oj



[quoted text, click to view]

Uri Dimant
9/4/2005 12:00:00 AM
Lisa
Is that Scalar UDF? Is that Inline Table-Valued UDF? Is that Multi-Statement
Table-Valued UDF?

Look at this technique written by Itzik Ben-Gan

CREATE FUNCTION dbo.fn_getinvid1() RETURNS int
AS
BEGIN
RETURN(SELECT newinvid FROM OPENQUERY([server_name],
'SET NOCOUNT ON; DECLARE @invid AS INT;
UPDATE tempdb..Seq SET @invid = val = val + 1; COMMIT;
SELECT @invid AS newinvid;') AS O)
END

CREATE FUNCTION dbo.fn_getinvid2() RETURNS int
AS
BEGIN
RETURN(
SELECT newinvid
FROM OPENQUERY(
[server_name],
'SET NOCOUNT ON;
INSERT INTO tempdb..Seq2 DEFAULT VALUES
ROLLBACK;
SELECT SCOPE_IDENTITY() AS newinvid;') AS O)
END

[quoted text, click to view]

Lisa
9/6/2005 7:55:07 AM
I'm looking at more in line with your first function. but I have two
parameters (one an integer) and one a date that is being to the function that
I want to use in the openquery statment. I can't get the sql right for it
though.



[quoted text, click to view]
AddThis Social Bookmark Button