Skip to content

Instantly share code, notes, and snippets.

@amogram
Last active August 29, 2015 14:08
Show Gist options
  • Save amogram/6906cf0988a46b6448e5 to your computer and use it in GitHub Desktop.
Save amogram/6906cf0988a46b6448e5 to your computer and use it in GitHub Desktop.
CREATE PROCEDURE [dbo].[UserAccount_Create]
(
@Name varchar(10)
, @FullName varchar(100)
, @LoginTitle varchar(100) = null
, @LoginMessage varchar(2000) = null
, @WelcomeMessage varchar(8000) = null
, @Domain varchar(100) = null
, @RegionId int = null
, @ExternalRef varchar(50) = null
, @Website varchar(1024) = null
-- logo
, @Data varbinary(max) = null
, @FileName varchar(256) = null
, @ContentType varchar(100) = null
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @@LocalTrans BIT
BEGIN TRY
SET @@LocalTrans = 0
IF( @@trancount = 0 )
BEGIN
-- only create a local transaction if no trnsactions already exists
SET @@LocalTrans = 1
BEGIN TRAN
END
declare @FileID int
if( @Data is not null)
begin
exec [File_Internal_Insert] @FileID output, @FileName, @Data, @ContentType, 4 /* Profile Photo */
end
insert into UserAccount
(
[Name]
, FullName
, LoginTitle
, LoginMessage
, WelcomeMessage
, RegionId
, Domain
, ExternalRef
, Website
, LogoFileID
)
values
(
@Name
, @FullName
, @LoginTitle
, @LoginMessage
, @WelcomeMessage
, @RegionId
, @Domain
, @ExternalRef
, @Website
, @FileID
)
declare @@Id int
set @@Id = scope_identity()
-- only commit local transactions
IF @@LocalTrans = 1
COMMIT TRAN
exec dbo.[UserAccount_Get] @@ID
END TRY
BEGIN CATCH
-- only roll back local transactions
IF @@LocalTrans = 1
ROLLBACK TRAN
-- raise the same error
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int, @ErrState int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY(),
@ErrState = ERROR_STATE()
RAISERROR(@ErrMsg, @ErrSeverity, @ErrState)
END CATCH
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment