Solved

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

Posted on 2011-09-13
3
231 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
ID: 36528166
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
ID: 36528184
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
ID: 36528199
Move the BEGiN so it is below the AS keyword
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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