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 2000 Forums
 Transact-SQL (2000)
 linked server OpenQuery Question help !!!!!!!!

Author  Topic 

nitinag
Starting Member

20 Posts

Posted - 2003-08-04 : 12:48:55
I am trying to make a remote function call inside one of my store procedure , only way I can do it is using openquery statement.
This works if I am passing no parameters however if I try to pass any parameter , it gives an error , I tried converting whole query to a string inlcuding conversions but it wouldn't work with parameters , Please help

Example that works

SELECT *
FROM OPENQUERY(CSESQL3, 'Select person_ID , email , name from CRM.dbo.fn_getEmailsList ( ''6912282'', 242760 )')



Example that does not works(This is actually required)

declare @t varchar(50)
declare @c int , @str varchar(1000)
set @c=242760
set @t='6912282'
Set @str =
'select * from CRM.dbo.fn_getEmailsList('+char(39)+@t+char(39)+', '+convert(varchar,@c)+')'

--Print @str
SELECT *
FROM OPENQUERY(CSESQL3, @str)





Please help ASAP.





tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-08-04 : 12:52:53
Instead of OPENQUERY, have you tried using the four part name? See the example below. Server1 would be a linked server. Dynamic sql will work when doing it this way.

SELECT *
FROM Server1.DBName.dbo.TableName

Tara
Go to Top of Page

nitinag
Starting Member

20 Posts

Posted - 2003-08-04 : 12:56:32
yes I did tried that , that works for stored procs only and not for functions ,
I need to return a table to which I can join in my local server. This didn't work for remote servers
Go to Top of Page

nitinag
Starting Member

20 Posts

Posted - 2003-08-04 : 12:58:06
Correction, it does not work for functions that need parameters to be passed in
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-08-04 : 13:17:54
From SQL Server Books Online:

"If the RETURNS clause specifies a TABLE type with columns and their data types, the function is a multi-statement table-valued function.

The following statements are allowed in the body of a multi-statement function. Statements not in this list are not allowed in the body of a function:

Assignment statements.

Control-of-Flow statements.

DECLARE statements defining data variables and cursors that are local to the function.

SELECT statements containing select lists with expressions that assign values to variables that are local to the function.

Cursor operations referencing local cursors that are declared, opened, closed, and deallocated in the function. Only FETCH statements that assign values to local variables using the INTO clause are allowed; FETCH statements that return data to the client are not allowed.

INSERT, UPDATE, and DELETE statements modifying table variables local to the function.

EXECUTE statements calling an extended stored procedures. "


------------------------------------------------

So I do not believe that you are going to be able to do this in a function. You will need to do this in a stored procedure instead.

Tara
Go to Top of Page
   

- Advertisement -