Solved

SQL Is there a way to re-write this stored procedure so it works?

Posted on 2011-09-13
3
222 Views
Last Modified: 2012-05-12
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[GetDeviceSettingsByDeviceID]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[GetDeviceSettingsByDeviceID];
GO
CREATE PROCEDURE [dbo].[GetDeviceSettingsByDeviceID]
(
     @DeviceID As int
)
AS

DECLARE @ChecklistEnabledCommand As varchar(50)
DECLARE @ChecklistEnabled As INT
DECLARE @TimeoutCommand As varchar(50)
DECLARE @Timeout As INT
DECLARE @TimeoutActionCommand As varchar(50)
DECLARE @TimeoutAction As varchar(50)
DECLARE @CanStartCommand As varchar(50)
DECLARE @CanStart As varchar(50)

SELECT @ChecklistEnabledCommand = 'CHKENDIS'
SELECT @ChecklistEnabled = (SELECT [ChecklistEnabled] FROM [501ChecklistSettings] WHERE [DeviceID] = @DeviceID)
SELECT @TimeoutCommand = 'CHUPTOUT'
SELECT @Timeout = (SELECT [Timeout] FROM dbo.[501ChecklistSettings] WHERE [DeviceID] = @DeviceID)
SELECT @TimeoutActionCommand = 'CUPOTOUT'
SELECT @TimeoutAction = (SELECT [TimeoutAction] FROM [501ChecklistSettings] WHERE [DeviceID] = @DeviceID)
SELECT @CanStartCommand = 'CHCANSTR'
SELECT @CanStart = (SELECT [CanStart] FROM [501ChecklistSettings] WHERE [DeviceID] = @DeviceID)


BEGIN
UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @ChecklistEnabledCommand,
            [Value] = @ChecklistEnabled,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @ChecklistEnabledCommand AND [Value] <> @ChecklistEnabled

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @ChecklistEnabledCommand,
        @ChecklistEnabled,
        'NEW'
     )

UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @TimeoutCommand,
            [Value] = @Timeout,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @TimeoutCommand AND [Value] <> @Timeout

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @TimeoutCommand,
        @Timeout,
        'NEW'
     )

UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @TimeoutActionCommand,
            [Value] = @TimeoutAction,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @TimeoutActionCommand AND [Value] <> @TimeoutAction

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @TimeoutActionCommand,
        @TimeoutAction,
        'NEW'
     )

UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @CanStartCommand,
            [Value] = @CanStart,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @CanStartCommand AND [Value] <> @CanStart

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @CanStartCommand,
        @CanStart,
        'NEW'
     )
END
GO
0
Comment
Question by:MBoy
3 Comments
 
LVL 5

Expert Comment

by:DerZauberer
Comment Utility
It's not so easy to understand what that SP is supposed to do if it works 'correctly'.
Do you get errors? Maybe add some explanation what this is supposed to mean ...
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
Comment Utility
yes SP looks good , only I think you need to change BEgin position to after AS

like below
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[GetDeviceSettingsByDeviceID]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[GetDeviceSettingsByDeviceID];
GO
CREATE PROCEDURE [dbo].[GetDeviceSettingsByDeviceID]
(
     @DeviceID As int
)
AS
BEGIN
DECLARE @ChecklistEnabledCommand As varchar(50)
DECLARE @ChecklistEnabled As INT
DECLARE @TimeoutCommand As varchar(50)
DECLARE @Timeout As INT
DECLARE @TimeoutActionCommand As varchar(50)
DECLARE @TimeoutAction As varchar(50)
DECLARE @CanStartCommand As varchar(50)
DECLARE @CanStart As varchar(50)

SELECT @ChecklistEnabledCommand = 'CHKENDIS'
SELECT @ChecklistEnabled = (SELECT [ChecklistEnabled] FROM [501ChecklistSettings] WHERE [DeviceID] = @DeviceID)
SELECT @TimeoutCommand = 'CHUPTOUT'
SELECT @Timeout = (SELECT [Timeout] FROM dbo.[501ChecklistSettings] WHERE [DeviceID] = @DeviceID)
SELECT @TimeoutActionCommand = 'CUPOTOUT'
SELECT @TimeoutAction = (SELECT [TimeoutAction] FROM [501ChecklistSettings] WHERE [DeviceID] = @DeviceID)
SELECT @CanStartCommand = 'CHCANSTR'
SELECT @CanStart = (SELECT [CanStart] FROM [501ChecklistSettings] WHERE [DeviceID] = @DeviceID)



UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @ChecklistEnabledCommand,
            [Value] = @ChecklistEnabled,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @ChecklistEnabledCommand AND [Value] <> @ChecklistEnabled

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @ChecklistEnabledCommand,
        @ChecklistEnabled,
        'NEW'
     )

UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @TimeoutCommand,
            [Value] = @Timeout,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @TimeoutCommand AND [Value] <> @Timeout

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @TimeoutCommand,
        @Timeout,
        'NEW'
     )

UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @TimeoutActionCommand,
            [Value] = @TimeoutAction,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @TimeoutActionCommand AND [Value] <> @TimeoutAction

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @TimeoutActionCommand,
        @TimeoutAction,
        'NEW'
     )

UPDATE [dbo].[DeviceSettings]
     SET
        [ItemName] = @CanStartCommand,
            [Value] = @CanStart,
            [State] = 'EDIT'
     WHERE
         [DeviceID] = @DeviceID AND [ItemName] = @CanStartCommand AND [Value] <> @CanStart

IF @@ROWCOUNT=0
    INSERT INTO  [DeviceSettings]
     (
            [DeviceID],
            [ItemName],
        [Value],
        [State]
     )
     VALUES
     (
            @DeviceID,
            @CanStartCommand,
        @CanStart,
        'NEW'
     )
END
GO

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
Move the BEGiN so it is below the AS keyword
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now