How to pass parameter to a store procedure in openrowset and sql-server authentication

445 views Asked by At

I have a store procedure with dynamic parameters (@ parametr1, @parametr2) and I wanna use it in an openrowset with SQL-SERVER authentication.

1

There are 1 answers

3
allmhuran On

Here's an example of how to do it with dynamic sql. Take care with the single apostrophes:

declare 
    @username sysname = N'MyUser', 
    @password nvarchar(32) = N'MyPassword', 
    @sqlinstance sysname = 'MYServer\MyInstance',
    @dbname sysname = 'MyDatabase',
    @query nvarchar(256);

set @query = concat
(
    N'select t.* from openrowset(''sqloledb'',''server=', @sqlinstance,
    ';Database=', @dbname,
    ';UID=', @username,
    ';Pwd=', @password,
    ';'', ''select somecolumn from someschema.sometable'') t'   
);
print @query;
exec(@query)