SQL SERVER連結伺服器執行帶參儲存過程
SQL SERVER通過連結伺服器,連結到ORACLE資料庫,下面我要在SQL SERVER資料庫上寫一個儲存過程,該儲存過程需要用通過連結服務去取ORACLE資料庫裡的資料,該儲存過程是含引數的儲存過程。在SQL SERVER 資料庫裡建立一個儲存過程來取ORACLE資料庫裡的一個表裡的資料如下:
1,在SQL SERVER資料庫上建立儲存過程
USE [ProdDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Test_Getdata_From_Oracle]
-- Add the parameters for the stored procedure here
@P_AS_OF_birthday datetime
AS
BEGIN
declare @E_SQL varchar(2000);
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
set @E_SQL = 'insert into test ' +
'select * from openquery(ORACLE11G, ''select * from test.test where birthday = '''''
+ CONVERT(varchar, @P_AS_OF_birthday, 111) + ''''' '') ';
print @E_SQL;
execute(@E_SQL);
END
GO
2,執行儲存過程
exec Test_Getdata_From_Oracle '1979-01-01'