?
Solved

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

Posted on 2011-09-13
3
Medium Priority
?
242 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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