Sunday, January 7, 2018

linked server Sql server OpenQuery by parameter example


DECLARE @SUBNAME NVARCHAR(100) ='محبوبه محمد  الشنقيطي'
DECLARE @SUBNO AS VARCHAR(100)='61319056000'

EXEC ('SELECT *  from CISWEB.CM_PER_NATIONAL_IQAMA_VW
WHERE  subnum = ?',  @SUBNO) AT [SAFALS];


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

COMPLETE EXAMPLE

ALTER PROCEDURE [dbo].[SAFA_GET_ACC_ID_BY_FILENO]
(
-- Add the parameters for the function here
@FILENO VARCHAR(10) ,
@ACCT_ID BIGint Output
)
AS

BEGIN
DECLARE @tmptable table (ACCT_ID VARCHAR(15))
--DECLARE @ACCT_ID VARCHAR(15) = '0'

insert into @tmptable
EXEC ('SELECT ACCT_ID  from CISADM.CI_ACCT_CHAR
WHERE  CHAR_TYPE_CD=''43036'' AND  ADHOC_CHAR_VAL = ?',  @FILENO) AT [SAFALS];

select @ACCT_ID = ACCT_ID from @tmptable
RETURN 

END



-------------------------------------------------------
CALL PROCEDURE


DECLARE @ACCT_ID bigint

EXEC [dbo].[SAFA_GET_ACC_ID_BY_FILENO]
@FILENO = N'80387',
@ACCT_ID = @ACCT_ID OUTPUT

SELECT @ACCT_ID as N'@ACCT_ID'

--SELECT 'Return Value' = @return_value

GO