views:

35

answers:

1

I'm trying to create a simple stored procedure which I can use to write data to a log table. I'm a noob in stored procedures, so what I'm missing here? I get an error saying:

Msg 201, Level 16, State 4, Procedure toLog, Line 0
Procedure or function 'toLog' expects parameter '@messageTime', which was not supplied.

How to modify this code to get this work?

USE <database>
GO
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
      WHERE TABLE_NAME = 'logTable')
   DROP TABLE dbo.logTable
GO
CREATE TABLE dbo.logTable (
    messageTime DATETIME NULL,
    etlJobName NVARCHAR(40) NULL,
    whereTo VARCHAR(10) NOT NULL,
    messag VARCHAR(255) NULL
)

/*************** procedure ************/
--DROP PROC dbo.toLog

CREATE PROCEDURE dbo.toLog
    @messageTime DATETIME,
    @etlJobName NVARCHAR(60) = 'unknown',
    @whereTo NVARCHAR(20) = 'print',
    @messag NVARCHAR(255) = 'empty'
AS
BEGIN
SET @messageTime = GETDATE()
IF @whereTo = 'log' OR @whereTo = 'logandprint'
    INSERT INTO logTable(messageTime, etlJobName, whereTo, messag)
    VALUES (@messageTime, @etlJobName, @whereTo, @messag)
IF @whereTo = 'print' OR @whereTo = 'logandprint'
    PRINT CONVERT(VARCHAR, GETDATE(), 113) + ', ' + @etlJobName + ', ' + @whereTo + ', ' + ', ' + @messag
END
GO

--execution
EXEC dbo.toLog @whereTo = 'log'
+1  A: 

It's because your toLog sproc is expecting you to supply the @messageTime parameter, which you haven't done, and it doesn't have a default value like the other parameters.

As you set the @messageTime always to GETDATE() inside the sproc, it looks like you just want to scrap that parameter:

CREATE PROCEDURE dbo.toLog
    @etlJobName NVARCHAR(60) = 'unknown',
    @whereTo NVARCHAR(20) = 'print',
    @messag NVARCHAR(255) = 'empty'
AS
BEGIN
IF @whereTo = 'log' OR @whereTo = 'logandprint'
    INSERT INTO logTable(messageTime, etlJobName, whereTo, messag)
    VALUES (GETDATE(), @etlJobName, @whereTo, @messag)
IF @whereTo = 'print' OR @whereTo = 'logandprint'
    PRINT CONVERT(VARCHAR, GETDATE(), 113) + ', ' + @etlJobName + ', ' + @whereTo + ', ' + ', ' + @messag
END

Or, if you do want to be able to pass in a datetime yourself:

CREATE PROCEDURE dbo.toLog
        @messageTime DATETIME = null,
        @etlJobName NVARCHAR(60) = 'unknown',
        @whereTo NVARCHAR(20) = 'print',
        @messag NVARCHAR(255) = 'empty'
    AS
    BEGIN
    IF (@messagetime IS NULL) SET @messagetime = GETDATE()

    IF @whereTo = 'log' OR @whereTo = 'logandprint'
        INSERT INTO logTable(messageTime, etlJobName, whereTo, messag)
        VALUES (@messageTime, @etlJobName, @whereTo, @messag)
    IF @whereTo = 'print' OR @whereTo = 'logandprint'
        PRINT CONVERT(VARCHAR, @MessageTime, 113) + ', ' + @etlJobName + ', ' + @whereTo + ', ' + ', ' + @messag
    END
AdaTheDev
Thanks, your secod solution produces an error:Msg 102, Level 15, State 1, Procedure toLog, Line 2Incorrect syntax near '('.Msg 137, Level 15, State 2, Procedure toLog, Line 8Must declare the scalar variable "@whereTo".Msg 137, Level 15, State 2, Procedure toLog, Line 10Must declare the scalar variable "@messageTime".Msg 137, Level 15, State 2, Procedure toLog, Line 11Must declare the scalar variable "@whereTo".Msg 137, Level 15, State 2, Procedure toLog, Line 12Must declare the scalar variable "@MessageTime".
atricapilla
@atricapilla - apologies, corrected now. Error was because you can't specify GETDATE() as a default for a param. So instead, it now defaults to the constant: null - which is then checked for in the sproc
AdaTheDev
Many thanks!....
atricapilla