Procedure or function 'TEST1 ' expects parameter '@StartDate1', which was not supplied

HI Experts !
I am trying to Insert the Records from Stored procedure to #TempTable . Finding the error.
The requirement is to create a stored procedure and insert the records by Passing the date parameters @StartDate1 & @EndDate1. Please help me out.

Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install.
Configuration option 'Ad Hoc Distributed Queries' changed from 1 to 1. Run the RECONFIGURE statement to install.
Msg 201, Level 16, State 4, Procedure TEST1, Line 0
Procedure or function 'TEST1' expects parameter '@StartDate1', which was not supplied
 CREATE PROCEDURE TEST1
        		@StartDate1 DATETIME OUTPUT,
        		@EndDate1 DATETIME OUTPUT
   AS

DECLARE @query nvarchar(max)
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

DECLARE @ProcessDate1 DATETIME
DECLARE @ProcessDate2 DATETIME

SET @StartDate=@StartDate1
SET @EndDate =@EndDate1


SET @ProcessDate1 = CONVERT(VARCHAR,dateadd(month,1+datediff(month,0,@StartDate),-1),101)  --- Run on any date will calculate  from first day to last date of the  month.
SET @ProcessDate2 =  CONVERT(VARCHAR,dateadd(month,1+datediff(month,0,@EndDate),-1),101)  --- Run on any date will calculate  from first day to last date of the  month.

SET @query = ''SELECT 
       convert(varchar,min([Start date]),101) 'Range start date',  
       convert(varchar,max([Start date]),101) 'Range end date'
From Table

 GO
sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
EXEC sp_serveroption 'COMPUTERNAME\MSSQLSERVER2012', 'DATA ACCESS', TRUE
SELECT *
INTO #TempTable
FROM OPENROWSET('SQLNCLI', 'Server=COMPUTERNAME\MSSQLSERVER2012;Trusted_Connection=yes;','EXEC DBName.[dbo].[TEST1]')
GO

.

I wouldn't truncate the datetime to date that way, just declare the variables as date. Use dynamic SQL for that.

You must pass in the dates to the proc. For example:

FROM OPENROWSET(..., ' EXEC DBName.[dbo].[TEST1] ''20181001'', ''20181005'' ')

[quote="ScottPletcher, post:3, topic:14139, full:true"]
You must pass in the dates to the proc. For example:

FROM OPENROWSET(..., ' EXEC DBName.[dbo].[TEST1] ''20181001'', ''20181005'' ')
[/quote]

Exactly I am missing the date parameters to pass runs fine:blush:...

I am trying to Pass declared parameters @startdate1 & Enddate1 instead of data ''20181001'', ''20181005''
but finding error message

DECLARE	
		@StartDate1 datetime,
		@EndDate1 datetime
		Set	@StartDate1 = '01/01/2017'   
            Set	@EndDate1 =  '01/31/2017'                
SELECT *
INTO #TempTable
FROM OPENROWSET('SQLNCLI', 'Server=COMPUTERNAME\MSSQLSERVER2012;Trusted_Connection=yes;','EXEC DBNAME.[dbo].[TEST1] @StartDate1,@EndDate1')



OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Deferred prepare could not be completed.".
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@StartDate1".

Looks like linked server issue

Test the linked server

Or

Drop recreate
If you can
But first ask administrator

I think there is no issue of linked server ,by passing dates directly the statement runs with no errors ...

Hi

https://support.microsoft.com/en-in/help/314520/how-to-pass-a-variable-to-a-linked-server-query

Hope this helps
,:blush::blush::blush::sunglasses::sunglasses::sunglasses:

A couple of different options, but these would require defining the temp table instead of relying on OPENROWSET. If possible, instead of using a procedure on the linked server - you could define the code you want to execute so you can control what is being returned.

Option 1:

Execute ('Execute dbname.dbo.test1 @startDate = ?, @endDate = ?', @ProcessDate11, @ProcessDate2) At linkedservername;

Option 2:

Execute linkedserver.dbname.dbo.test1 @startDate = @ProcessDate1, @endDate = @ProcessDate2;

Option 3:

Declare @sqlCommand varchar(max) = 'Execute dbname.dbo.test1 @startDate = ?, @endDate = ?';
Execute (@sqlCommand, @ProcessDate1, @ProcessDate2) At linkedserver;

Each option will require building the temp table first...then using INSERT INTO #temp {execute statement here};

With Option 3 - you could dynamically build the code to sent instead of using a stored procedure on the linked server which gives you the ability to control what columns are returned. This way - you would not have any issues if someone modified the stored procedure you are calling and any changes to data elements being returned are controlled by your procedure.

Thank all for your suggestion & Idea really Appreciated..

Finally I came up to get rid OPENROWSET problem:relaxed:

 SELECT *
INTO #TempTable
FROM OPENROWSET('SQLNCLI', 'Server=COMPUTERNAME\MSSQLSERVER2012;Uid=sa;Pwd=PASSWORD;Database=DBNAME;Trusted_Connection=yes;',
' set fmtonly off;
DECLARE	@StartDate1 datetime,
		@EndDate1 datetime
SET	@StartDate1 = ''1/1/2018''    
SET	@EndDate1 =  ''1/31/2018''   
EXEC  DBNAME.[dbo].[TEST1] @StartDate1 , @EndDate1
')