| Author |
Topic  |
|
|
rsquared
Starting Member
5 Posts |
Posted - 10/18/2012 : 12:00:19
|
| I need to write a stored procedure that will change or update a boolean field to true if it is false, and false if it is true. In order to update the correct record, I need to use the record number as a parameter.Thanks ! |
|
|
Lamprey
Flowing Fount of Yak Knowledge
3833 Posts |
|
|
rsquared
Starting Member
5 Posts |
Posted - 10/18/2012 : 14:11:32
|
quote: Originally posted by Lamprey
What have you tried so far? Can you post your DDL & DML (expected output would be nice too). Here are some links that can help you prepare that: http://www.sqlservercentral.com/articles/Best+Practices/61537/ http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
ALTER PROCEDURE [dbo].[spCheckActivePaient] -- Add the parameters for the stored procedure here @MR varchar(50) AS
-- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON
-- Insert statements for procedure here
SELECT CASE WHEN Active = 1 UPDATE Set Active= 0 ELSE UPDATE Set Active = 1 END , LastName,Active FROM Demographics WHERE MR=@MR |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47189 Posts |
Posted - 10/18/2012 : 14:18:13
|
ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Insert statements for procedure here
SELECT ~Active , LastName,Active
FROM Demographics
WHERE MR=@MR
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
rsquared
Starting Member
5 Posts |
Posted - 10/18/2012 : 14:32:22
|
quote: Originally posted by visakh16
ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Insert statements for procedure here
Thanks but where is the part that will determine if Active is true? And if it is true I want to update it to false????
SELECT ~Active , LastName,Active
FROM Demographics
WHERE MR=@MR
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47189 Posts |
Posted - 10/18/2012 : 14:56:16
|
quote: Originally posted by rsquared
quote: Originally posted by visakh16
ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Insert statements for procedure here
Thanks but where is the part that will determine if Active is true? And if it is true I want to update it to false????
SELECT ~Active , LastName,Active
FROM Demographics
WHERE MR=@MR
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
~Active will cause 1 to become 0 and 0 to become 1 . no need of any checking conditions. ~ is unary not operator
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
rsquared
Starting Member
5 Posts |
Posted - 10/18/2012 : 15:10:31
|
quote: Originally posted by visakh16
quote: Originally posted by rsquared
quote: Originally posted by visakh16
ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Insert statements for procedure here
Thanks but where is the part that will determine if Active is true? And if it is true I want to update it to false????
SELECT ~Active , LastName,Active
FROM Demographics
WHERE MR=@MR
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
~Active will cause 1 to become 0 and 0 to become 1 . no need of any checking conditions. ~ is unary not operator
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
I don't get it. It is not changing the values form 0 to 1???? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47189 Posts |
Posted - 10/18/2012 : 15:30:36
|
are you asking about changing values in tables or just changing why displaying data?
If former then it should be an update like
ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- Insert statements for procedure here
UPDATE d
SET Active = ~Active
FROM Demographics d
WHERE MR=@MR
If latter then my posted suggestion works
here's an illustration for you as well
declare @test table
(
i bit,
r int
)
insert @test
values (1,123)
insert @test
values (0,110)
insert @test
values (1,234)
select i,~i
from @test
where r=123
output
-------------------
i Negatedi
-------------------
1 0
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
Edited by - visakh16 on 10/18/2012 15:31:17 |
 |
|
|
rsquared
Starting Member
5 Posts |
Posted - 10/18/2012 : 17:55:21
|
| It worked great. Thank for your help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47189 Posts |
Posted - 10/18/2012 : 21:42:44
|
welcome
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|