Results 1 to 14 of 14

Thread: [RESOLVED] Formal parameter was defined as OUTPUT but the actual parameter not declared OUTPUT.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Resolved [RESOLVED] Formal parameter was defined as OUTPUT but the actual parameter not declared OUTPUT.

    Hello,

    I am trying to call a stored procedure in MSSQL from VB6; however, I am getting an error message: "Formal parameter was defined as OUTPUT but the actual parameter not declared OUTPUT."

    I have looked at examples online and within these threads, but I cannot see what is incorrect in the code. I am hoping someone can help me out?


    Below is the code in MSSQL for the Stored Procedure:
    Code:
    CREATE PROCEDURE [dbo].[SP_ExportData] 
    
        @sql varchar(5000) = 'Select * from vw_WIP_Report',        
        @fullFileName varchar(1000) = '\\na.ina.com\fortmill\DATA\NZ-IBC-Z\Projects\02_BAM\Eng_Dev\90_To_Be_Deleted\WIP_Report\WipReport.csv',
        @ReturnValue int = 1 OUTPUT
    
    as
    
    BEGIN
       	if @sql = '' or @fullFileName = ''
    	begin
    		select 0 as ReturnValue -- failure
    		return
    	end 
    
    
    	if object_id('##TempExportData') is not null
    		drop table #TempExportData
    	if object_id('##TempExportData2') is not null
    		drop table #TempExportData2
    
    
    	-- insert data into a global temp table
    	declare @columnNames varchar(8000)
              , @columnNamesBrackets varchar(8000)
              , @columnNamesQuotes varchar(8000)
              , @columnConvert varchar(8000)
              , @tempSQL varchar(8000)
              ,@ReturnCode int
              ,@ErrorMessage varchar(1000)
    
    
    	select    @tempSQL = left(@sql, charindex('from', @sql)-1) + ' into ##TempExportData ' + 
    		 substring(@sql, charindex('from', @sql)-1, len(@sql))
         --/* Debugging Statement    
        print @tempSQL
        --*/
      
    	exec(@tempSQL)
    
        /* Debugging Statement    
        select * from ##TempExportData
        --*/
    
    	if @@error > 0
    	begin
    		select @ReturnValue = 0  -- failure
    		return
    	end 
    
    
    	-- build 2 lists
    	-- 1. column names
    	-- 2. columns converted to nvarchar
    	SELECT  @columnNamesBrackets = COALESCE( @columnNamesBrackets  + ',', '') + '[' + column_name + ']',
    			@columnNamesQuotes = COALESCE( @columnNamesQuotes  + ',', '') + '''' + column_name + '''',
    			@columnConvert = COALESCE( @columnConvert  + ',', '') + ' convert(nvarchar(4000),' 
    			+ ' [' + column_name + ']' + case when data_type in ('datetime', 'smalldatetime') then ',121'
    								 when data_type in ('numeric', 'decimal') then ',128'
    								 when data_type in ('float', 'real', 'money', 'smallmoney') then ',2'
    								 when data_type in ('datetime', 'smalldatetime') then ',120'
    								 else ''
    							end + ') as ' + '''' + column_name + ''''
    	FROM    tempdb.INFORMATION_SCHEMA.Columns
    	WHERE    table_name = '##TempExportData'
    
    
        /* Debugging Statement    
        print @columnNames
         --*/
    
    	-- execute select query to insert data and column names into new temp table
    	SELECT    @sql = 'select ' + @columnNamesBrackets + ' into ##TempExportData2 from (select ' + @columnConvert + ', ''2'' as [temp##SortID]        
                  from ##TempExportData union all select ' + replace(@columnNamesQuotes, ', ', ', ') + ', ''1'') t order by [temp##SortID]'
    
        /* Debugging Statement    
        print @sql
        --*/
    
    
    	exec (@sql)
    
    
        --/* Debugging Statement    
        Select * from ##TempExportData2
        --*/
    
    	-- build full BCP query
    	select    @sql = 'bcp "select * from ##TempExportData2" queryout "' + @fullFileName + '" -T -c -t, '
        print @sql
    
        /* Debugging Statement
        select * from ##TempExportData2
        --*/
        
    	--Create temp table to hold result
        CREATE TABLE #temp (SomeCol VARCHAR(500))
        CREATE TABLE #ErrorTable ( rownumber int Identity(1,1)
                                 , ErrorMsg varchar(8000))
    	-- execute BCP
    	INSERT #temp
        Exec @ReturnCode = master..xp_cmdshell @sql
    
    	IF @ReturnCode <> 0
    	BEGIN
    		SELECT @ErrorMessage = @ErrorMessage 
    		FROM #temp
    
    		--Display error message and return code
    	  INSERT into #ErrorTable
          SELECT SomeCol from #temp where SomeCol like 'Error =%' 
    	 
          Select * from #ErrorTable
    	END
    
    	if @@error > 0
    	begin 
    		select @ReturnValue = 0 -- failure
    		return
    	end
    
    	drop table #temp
    	drop table #ErrorTable
    	drop table ##TempExportData
    	drop table ##TempExportData2
    	select  @ReturnValue = 1 -- success
    
    END
    
    GO
    And below is the VB6 code:
    Code:
    Sub export()
        Dim res As Integer
        Set cmd = New ADODB.Command
        cmd.ActiveConnection = cnConn
        cmd.CommandType = adCmdStoredProc
        cmd.CommandText = "SP_ExportData"
        cmd.Parameters.Append cmd.CreateParameter("@ReturnValue", adInteger, adParamOutput)
        cmd.Execute ' This is where the error is thrown.
        res = cmd("@ReturnValue")
        If (res = 1) Then
            MsgBox "Exported Successfully"
        End If
        Set cmd.ActiveConnection = Nothing
    End Sub
    Any help I can get would be much appreciated! Thanks again!!

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    I'm not well up on MSSQL but is there an '@' missing before 'ReturnValue' here:
    Code:
    select 0 as ReturnValue -- failure

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    More specifically, it should look like this:
    Code:
    SET @ReturnValue = 0
    Return
    -- or
    SELECT @ReturnValue = 0
    Return
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Why do you have this

    Code:
    CREATE PROCEDURE [dbo].[SP_ExportData] 
    
        @sql varchar(5000) = 'Select * from vw_WIP_Report',        
        @fullFileName varchar(1000) = '\\na.ina.com\fortmill\DATA\NZ-IBC-Z\Projects\02_BAM\Eng_Dev\90_To_Be_Deleted\WIP_Report\WipReport.csv',
        @ReturnValue int = 1 OUTPUT
    
    as
    
    BEGIN
    Shouldn't that be:

    Code:
    CREATE PROCEDURE [dbo].[SP_ExportData] 
    
        @ReturnValue int  OUTPUT
    
    as
    
    BEGIN
        Declare @sql varchar(5000)
        Set @sql = 'Select * from vw_WIP_Report'
        Declare @fullFileName varchar(1000)
        Set @fullFileName  = '\\na.ina.com\fortmill\DATA\NZ-IBC-Z\Projects\02_BAM\Eng_Dev\90_To_Be_Deleted\WIP_Report\WipReport.csv'

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Depends... the first way allows the other items to be passed, or set with default values if no values are specified... but at the same time... the OP isn't creating the parameters in code... which I think you still need to do... even if you don't set their values.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Thanks for the great replies! I did not code the MSSQL statements, but I can tell you that techgnome's most recent statements concerning it are correct. The code is meant to be used dynamically.

    My biggest concern was the VB6 code. I will make the proposed changes, and let you know if I have any issues.

    Thanks again!

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Quote Originally Posted by techgnome View Post
    Depends... the first way allows the other items to be passed, or set with default values if no values are specified... but at the same time... the OP isn't creating the parameters in code... which I think you still need to do... even if you don't set their values.

    -tg
    Can you tell me what you mean by "the OP isn't creating the parameters in code"? Does OP mean Output Parameter?

    The error message that I am getting is "Formal parameter '@sql' was defined as OUTPUT but the actual parameter not declared OUTPUT". I do not understand why it keeps referencing @sql.

    Any help would be great! Thanks again!

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    You are the OP - the ORIGINAL POSTER.

    Why do you have 3 parameters in the SPROC but you are only creating one on the VB side?

    You either make the VB have three parameters or you move those parameters down to inside the SPROC.

    They should match.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Show the VB6 code with the 3 parameters please. Actually whenever you post that you changed something and you are still getting an error - please post the code.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Well, again, I do not know MSSQL syntax very well, but when I do not pass a parameter, then I still receive the same error.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Having updated the VB code to include the other parameters, I am still receiving the same error message at cmd.Execute.

    Below is the update VB code with the additional parameters being passed:
    Code:
    Sub export()
        Dim res As Integer
        Set cmd = New ADODB.Command
        cmd.ActiveConnection = cnConn
        cmd.CommandType = adCmdStoredProc
        cmd.CommandText = "SP_ExportData2"
        cmd.Parameters.Append cmd.CreateParameter("@sql", adVarChar, adParamOutput, 1000)
        cmd.Parameters.Append cmd.CreateParameter("@fullFileName", adVarChar, adParamOutput, 1000)
        cmd.Parameters.Append cmd.CreateParameter("@ReturnValue", adInteger, adParamOutput)
        cmd.Execute
        res = cmd("@ReturnValue")
        If (res = 1) Then
            MsgBox "Exported Successfully"
        End If
        Set cmd.ActiveConnection = Nothing
    End Sub
    And below is the corresponding MSSQL code:
    Code:
    CREATE PROCEDURE [dbo].[SP_ExportData2] 
    
        @sql varchar(5000) = 'Select * from vw_WIP_Report',        
        @fullFileName varchar(1000) = '\\na.ina.com\fortmill\DATA\NZ-IBC-Z\Projects\02_BAM\Production\90_To_Be_Deleted\WIP_Report\WipReport.csv',
        @ReturnValue int = 1 OUTPUT
    
    as
    
    BEGIN
    Could the problem be with the adVarChar?

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    You have adParamOutputfor all three parameters in the VB.

    But the SQL only has the third parameter as OUTPUT.

    This needs to match - fix the VB side and remove the adParamOutput

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Smile Re: Formal parameter was defined as OUTPUT but the actual parameter not declared OUTP

    This works correctly now. Below are the code fragments.

    Since I wanted to pass a parameter to the stored procedure in MSSQL for verification purposes, and since the stored procedure was set up to accept three parameters, I had to send all three. The first two were only input parameters, and the last one was the output parameter that I test on. Hopefully, this will help those that experience similar problems.


    Code:
    CREATE PROCEDURE [dbo].[SP_ExportData] 
    
        @sql varchar(5000) = 'Select * from vw_WIP_Report',        
    	@fullFileName varchar(1000) = '\\na.ina.com\fortmill\DATA\NZ-IBC-Z\Projects\02_BAM\Production\90_To_Be_Deleted\WIP_Report\WipReport.csv',
        @ReturnValue int = 1 OUTPUT
    
    as
    
    
    
    BEGIN
    Code:
    Sub export()
        Dim res, i As Integer
        Set cmd = New ADODB.Command
        cmd.ActiveConnection = cnConn
        cmd.CommandType = adCmdStoredProc
        cmd.CommandText = "SP_ExportData"
        cmd.Parameters.Append cmd.CreateParameter("@sql", adVarChar, adParamInput, 5000)
        cmd.Parameters.Append cmd.CreateParameter("@fullFileName", adVarChar, adParamInput, 1000)
        cmd.Parameters.Append cmd.CreateParameter("@ReturnValue", adInteger, adParamOutput)
        cmd.Execute
        res = cmd("@ReturnValue")
        If (res = 1) Then
            i = MsgBox("The CSV file is available at the following folder location: " & vbCrLf & vbCrLf & "\\na.ina.com\fortmill\DATA\NZ-IBC-Z\Projects\02_BAM\Production\90_To_Be_Deleted\WIP_Report\", vbInformation, "Process Complete")
        Else
            i = MsgBox("The CSV file was NOT created successfully.  Contact the P & I department.", vbCritical, "Process Failed")
        End If
        Set cmd.ActiveConnection = Nothing
    End Sub
    Thanks for everyone who contributed! It was greatly appreciated!

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width