Hi everyone,
I've hit a roadblock with some SQL code and need some help from some SQL guru's. I have a trigger on my tps_user table that will set the network login based on the domain that is imported for a user via AD. I've give it below:
USE [Database_Name]
GO
/****** Object: Trigger [dbo].[addnetworklogin] Script Date: 06/17/2013 16:20:33 ******/
SETANSI_NULLSON
GO
SETQUOTED_IDENTIFIERON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTERTRIGGER [dbo].[addnetworklogin]
ON [dbo].[tps_user]
AFTERINSERT
AS
begin
declare @Name asvarchar(255)
declare @Domain asvarchar(255)
declare @guid uniqueidentifier
declare @networkLogin asvarchar(255)
declare @count int
declare UserCursor cursorfor
SELECT tps_guid, tps_name, usr_domain
FROM tps_user
open UserCursor
fetchnextfrom UserCursor into @guid, @Name, @Domain
while (@@FETCH_STATUS= 0)
begin
set @networkLogin = @Domain +'\'+ @Name
set @count =''
select @count =count(*)from tps_user_network_login
where tps_network_login = @networkLogin
groupby tps_network_login
print'User: '+@networkLogin+', count: '+convert(varchar(10),@count)
if @count =''
begin
-- uncomment the following line if you want to limit all users to 1 network login.
-- delete from tps_user_network_login where tps_user_guid = @guid
insertinto tps_user_network_login (tps_guid,tps_user_guid,tps_network_login)
values (newid(),@guid,@networkLogin)
end
elseupdate tps_user_network_login set tps_user_guid = @guid where tps_network_login = @networkLogin
fetchnextfrom UserCursor into @guid, @Name, @Domain
end
close UserCursor
deallocate UserCursor
end
The code works great with one exception... if I am allowing the system to add accounts automatically if it doesn't recognize the email I get an error that it cannot allow NULLs. I need to update this code to allow a static value to be added as the prefix for @domain for these cases. I know it has to be pretty simple but I just don't know where to put it.
So what I need is:
if domain is NULL then network login should be "static_Domain\name"
else the rest of the stuff that works.
Thanks!