Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-13
3
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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