﻿CREATE PROCEDURE [dbo].[sp_insertClienteIntoDB]
@codiceFiscale as varchar(16),
@utente as varchar(13),
@nome as varchar(50) = null,
@cognome as varchar(50) = null
AS
BEGIN
SET NOCOUNT ON;
DECLARE @found as int
--esiste gia?
SELECT @found = count(*) FROM [dbo].[Cliente] WHERE [CodFiscale] = @codiceFiscale
 IF(@found <= 0)  --non esiste
	 INSERT INTO [dbo].[Cliente]
			   ([DtCreazione]
			   ,[CodEsterno]
			   ,[CodFiscale]
			   ,[Utente]
			   ,[Cognome]
			   ,[Nome]
			   ,[Nominativo])
		 VALUES
			   (getdate()
			   ,@codiceFiscale
			   ,@codiceFiscale
			   ,@utente
			   ,@cognome
			   ,@nome
			   ,case when len(@cognome)> 49 then @cognome else @nome + ' ' + @cognome END)
    END