May 10, 2015 at 4:43 am
Hello,
I have 2 stored Procedures
the first get the UserId by UserName parameter
the second Delete the user from multiple tables by UserId
ALTER PROCEDURE [dbo].[GetUserIDbyUserName]
@UserName nvarchar(100)
AS
SELECT
[UserId]
FROM
[Users]
WHERE
[UserName] = @UserName
SET QUOTED_IDENTIFIER OFF
the Second
ALTER PROCEDURE [dbo].[DeleteUserByUserName]
@UserName nvarchar(100)
AS
declare @user-id uniqueidentifier
exec @user-id = [dbo].[GetUserIDbyUserName] @UserName
delete from Memberships where UserId = @user-id
delete from UsersInRoles where UserId = @user-id
delete from Users where UserId = @user-id
I want to send the username only to the sencond procedure to do everything, Please tell what I'm missing in the previous codes
May 10, 2015 at 5:23 am
Quick suggestion, use OUTPUT parameter for the procedure
😎
/* Procedure #1 */
CREATE PROCEDURE [dbo].[GetUserIDbyUserName]
@UserName nvarchar(100)
,@UserID uniqueidentifier = NULL OUTPUT
AS
SELECT
@user-id = [UserId]
FROM
[Users]
WHERE
[UserName] = @UserName
GO
/* Procedure #2 */
CREATE PROCEDURE [dbo].[DeleteUserByUserName]
@UserName nvarchar(100)
AS
declare @user-id uniqueidentifier
exec [dbo].[GetUserIDbyUserName] @UserName ,@UserID OUTPUT
delete from Memberships where UserId = @user-id
delete from UsersInRoles where UserId = @user-id
delete from Users where UserId = @user-id
GO
May 10, 2015 at 7:39 am
Thanks for the quick and easy reply 🙂
Appreciated
May 10, 2015 at 4:13 pm
Hmmmm.... I recognize those tables. I would strongly recommend NEVER deleting from those tables because 1) you could orphan a whole lot of information that many applications rely on for "who dunnit" and 2) any auditors that may pay you a visit will flunk you immediately for tampering with historical information. There are columns in some of the tables (like the Users table and others) that will disable logins/users but you should never delete from those tables.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply