0

I have a stored procedure in a SQL Project running in JetBrains Rider which is defined as follows:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[spGetDeviceNotification]
    @SerialNumber NVARCHAR(60),
    @ChannelID INT,
    @DownloadTableName VARCHAR(200)
AS
    -- Notes for testing
    -- @SerialNumber and @ChannelID are set as default values for testing
    -- But these can be overwritten
    /******************** 
     * Declare Variables *
     *********************/
    DECLARE
        @ErrorLine                  INT,
        @ErrorState                 INT,
        @ErrorNumber                INT,
        @ErrorMessage               NVARCHAR(4000),
        @ErrorSeverity              INT,
        @ErrorProcedure             NVARCHAR(200),
        @RC                         INT,
        @InputMessage               NVARCHAR(4000),
        @TableName                  NVARCHAR(60),
        @SQLX                       NVARCHAR(4000),
        @ArchiveInputFileName       NVARCHAR(1000),
        @ImportDelimiter            VARCHAR(10),
        @FileNameTimestamp          NVARCHAR(100),
        @FromDate                   DATETIME2,
        @ToDate                     DATETIME2,
        @Procname                   NVARCHAR(200),
        @CreatedByUser              UNIQUEIDENTIFIER

    DECLARE @ErrorMessage_OUT       NVARCHAR(4000)

    /*********************** 
    * Initialise Variables *
    ***********************/

    SET @ErrorLine                  = 0
    SET @ErrorState                 = 1
    SET @ErrorNumber                = 50000
    SET @ErrorMessage               = N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' + 'Message: '
    SET @ErrorSeverity              = 16
    SET @ErrorProcedure             = ISNULL(OBJECT_NAME(@@PROCID), '-')
    SET @ProcName                   = @ErrorProcedure
    SET @RC                         = 0
    SET @CreatedByUser              = [dbo].[fnReturnUserID] (SYSTEM_USER)

    -- Extract FromDate from the first date in readin reading table
    SET @sqlx = 'SELECT  @FromDate = MIN(ReadingDateTime) FROM ' + Quotename(@DownloadTableName,'[]')
    EXEC [dbo].[sp_executesql]   @sqlx, N'@FromDate datetime out', @FromDate out

    -- Extract FromDate from the first date in readin reading table
    SET @sqlx = 'SELECT  @ToDate = MAX(ReadingDateTime) FROM ' + Quotename(@DownloadTableName,'[]')
    EXEC [dbo].[sp_executesql]   @sqlx, N'@ToDate datetime out', @ToDate out

    SET @FromDate   =   CONVERT(DATETIME2, CONVERT(varchar(11),@FromDate, 111 ) + ' 00:00:00', 111)
    SET @ToDate     =   CONVERT(DATETIME2, CONVERT(varchar(11),@ToDate, 111 ) + ' 23:59:59', 111)

    BEGIN TRY
        SELECT
            dn.DeviceNotificationID,
            Dce.SerialNumber,
            dc.ChannelID,
            Dn.NotificationDate,
            Dn.NotificationXML
        FROM
            [dbo].[Device] Dce
        JOIN
            [dbo].[DeviceChannel] Dc ON Dce.DeviceID = Dc.DeviceID
        JOIN
            [dbo].[DeviceNotification] Dn ON DC.DeviceChannelID = dn.DeviceChannelID
        WHERE
            SerialNumber =  @SerialNumber
            AND ChannelID = @ChannelID
            AND NotificationDate >= ISNULL(@FromDate, NotificationDate)
            AND NotificationDate <= ISUNLL(@ToDate, NotificationDate)
        ORDER BY 
            NotificationDate

     END TRY
     BEGIN CATCH
         -- Get Error Variables Values      
         SET @ErrorNumber    = ERROR_NUMBER()
         SET @ErrorSeverity  = ERROR_SEVERITY()
         SET @ErrorState     = ERROR_STATE()
         SET @ErrorLine      = ERROR_LINE()
         SET @ErrorProcedure = @ProcName  -- removed Error Procedure because it returns blank 

         -- Build ErrorMessage Variable Value      
         SET @ErrorMessage = @ErrorMessage + ERROR_MESSAGE()
         SET @InputMessage = @ProcName + ': Process Failed. '+ @ErrorMessage

         EXECUTE [Aud].[spSetSystemLog]
                       @SpName = @ProcName,
                       @Arguments = NULL,
                       @SystemLogMessage = @InputMessage,
                       @DMLRowsAffected = NULL,
                       @UserID = @CreatedByUser
  
         RAISERROR (@ErrorMessage, @ErrorSeverity, 1, @ErrorNumber, @ErrorSeverity, @ErrorState, @ErrorProcedure, @ErrorLine) 
         -- Exit stored procedure 
     END CATCH
 
     SET NOCOUNT OFF

    -- Exit stored procedure
    RETURN @RC

Now upon building the SQL Project, I get the following warning:

spGetDeviceNotification.sql(95, 10): [SQL71502] SqlProcedure: [dbo].[spGetDeviceNotification] has an unresolved reference to object [dbo].[sp_executesql].

How can I get rid of this warning in this database project in JetBrains Rider?

I saw some suggestions of creating a database reference to the master database... this can be easily done in Visual Studio but not so easy in Rider.

But what I did was to amend the .csproj file for my SQL project and added the following lines:

<ItemGroup>
        <ArtifactReference Include="$(DacPacRootPath)\Extensions\Microsoft\SQLDB\Extensions\SqlServer\130\SqlSchemas\master.dacpac">
            <HintPath>$(DacPacRootPath)\Extensions\Microsoft\SQLDB\Extensions\SqlServer\130\SqlSchemas\master.dacpac</HintPath>
            <SuppressMissingDependenciesErrors>True</SuppressMissingDependenciesErrors>
            <DatabaseVariableLiteralValue>master</DatabaseVariableLiteralValue>
        </ArtifactReference>
    </ItemGroup>

But still the SQL warning is there... How can I get rid of a warning for unresolved reference to object [dbo].[sp_executesql]?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
ChrisAsi71
  • 41
  • 4
  • 3
    I know nothing of Rider, but what happens if you properly schema qualify the system object? `sp_executesql` isn't on the `dbo` schema, it's on the `sys` schema. – Thom A Sep 01 '23 at 15:13
  • 1
    `sp_executesql` is a system stored procedure. It's not in the `dbo` schema – Panagiotis Kanavos Sep 01 '23 at 15:17
  • 1
    As a completely separate note, syntax like `NotificationDate <= IsNull(@ToDate,NotificationDate)` isn't advisable. You are better off using an `OR` :` `(NotificationDate <= @ToDate OR @ToDate IS NULL)`. You will then likely want to add `RECOMPILE` to your `OPTION` clause. – Thom A Sep 01 '23 at 15:24
  • I tried qualifying the system object but got the following warning: spGetDeviceNotification.sql(70, 10): [SQL71502] SqlProcedure: [dbo].[spGetDeviceNotification] has an unresolved reference to object [sys].[sp_executesql]. – ChrisAsi71 Sep 01 '23 at 15:29
  • What happens if you fix the SP syntax error line `AND NotificationDate <= ISUNLL(@ToDate, NotificationDate)` to `AND NotificationDate <= ISNULL(@ToDate, NotificationDate)` – Mark Schultheiss Sep 01 '23 at 18:15
  • Your datetime to/from code is not great also as times like `23:59:59.123` will be missed. Why not `DATEADD(DAY,1, CAST(@ToDate AS DATE))` then do `AND NotificationDate < @ToDate` – Mark Schultheiss Sep 01 '23 at 18:28
  • What does Rider use to do the build? Relevant? https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/issues/64 – Martin Smith Sep 01 '23 at 23:17
  • I believe the warning is coming SSDT, not JetBrains, because it doesn't recognize `sys` objects at build time even though they're perfectly valid at runtime. Have you tried to add a Database Reference to your project for `master` so that SSDT can find the `sys` objects? – AlwaysLearning Sep 02 '23 at 04:51

0 Answers0