How to change = 0 to IS NULL - sql

Below I am using OUTPUT Parameters to output an update statement:
UPDATE set Value = src.Value, DeletedDate = iif (#Deleted =1, getutcdate(), NULL), ModifiedBy = SUSER_SNAME(), ModifiedDate = getutcdate()
WHEN NOT MATCHED THEN
INSERT (ConfigKey, Value, AddedBy, ModifiedBy)
VALUES (#ConfigKey, #Value, SUSER_SNAME(), SUSER_SNAME())
OUTPUT 'Core.Config', 'ConfigID', inserted.ConfigID,
iif($action = 'Update', Core.updXMLFragment('Value', inserted.Value, deleted.Value)+
Core.updXMLFragment('ModifiedBy', inserted.ModifiedBy, deleted.ModifiedBy) +
Core.updXMLFragment('DeletedDate', inserted.DeletedDate, deleted.DeletedDate)
, Core.insXMLFragment('ConfigID') +
Core.addnlXMLFragment ('ModifiedBy', inserted.ModifiedBy))
The code above generates the update statement at the bottom, but the issue I have is 'DeletedDate = 0' in the update statement. I need this to be changed to DeletedDate=NULL and in the condition 'DeletedDate IS NULL'. How can this be done?
BEGIN TRAN
update Core.Config
set Value = 'Insert',ModifiedBy = 'HARROGATE\Name' ,DeletedDate = '0'
where ConfigID = '1' and Value = 'Update' and ModifiedBy = 'HARROGATE\Name' and DeletedDate = '0'
if ##ROWCOUNT <> 1
BEGIN;
THROW 99999, 'Error - Single Row not updated, rollback terminated', 1;
END;
UPDATE: Included updxmlfragment
BEGIN
if (#InsertedValue = '')
BEGIN
set #InsertedValue = ' '
END
if(#DeletedValue = '')
BEGIN
set #DeletedValue = ' '
END
if(#FieldName IS NULL)
--if(#InsertedValue IS NULL and #DeletedValue IS NULL or #FieldName IS NULL) -- pre v2.1 logic
BEGIN
-- This "errors" the function and ensures the script doesn't get any further than the update
return convert(int, 'NULL Params passed for XML Fragment Generation')
END
-- Validate the date has the correct "time" element associated - i.e HH:MM:SS:MSS
if (#FieldName like '%date%')
BEGIN
if((TRY_PARSE(#InsertedValue as datetime) IS NOT NULL) or (TRY_PARSE(#DeletedValue as datetime) IS NOT NULL))
BEGIN
if((len(replace(#DeletedValue, ':', '')) +3 <> len(#deletedvalue)) OR (len(replace(#InsertedValue, ':', '')) +3 <> len(#InsertedValue)))
BEGIN
-- This "errors" the function and ensures the script doesn't get any further than the update
return convert(datetime, 'Invalid Date Params passed for XML Fragment Generation')
END
END
END
return
'<Update><FieldName>'+#FieldName+'</FieldName>'+iif(#DeletedValue is null, '<OldValue />', '<OldValue><![CDATA['+#DeletedValue+']]></OldValue>')
+iif(#InsertedValue is null, '<NewValue />', '<NewValue><![CDATA['+#InsertedValue+']]></NewValue>') + '</Update>
'

Why not to use NULLIF()?
Returns a null value if the two specified expressions are equal.
So you'd have to use NULLIF(DeletedDate, 0) instead of DeletedDate only.
Update
As per my understanding, you have to use it like that:
UPDATE
SET Value = src.Value
, DeletedDate = IIF(#Deleted = 1, getutcdate(), NULL)
, ModifiedBy = SUSER_SNAME()
, ModifiedDate = getutcdate() WHEN NOT MATCHED THEN
INSERT (ConfigKey, Value, AddedBy, ModifiedBy)
VALUES (#ConfigKey, #Value, SUSER_SNAME(), SUSER_SNAME())
OUTPUT 'Core.Config'
, 'ConfigID'
, inserted.ConfigID
, IIF($ACTION = 'Update', Core.updXMLFragment('Value', inserted.Value, deleted.Value) + Core.updXMLFragment('ModifiedBy', inserted.ModifiedBy, deleted.ModifiedBy) + Core.updXMLFragment('DeletedDate', NULLIF(inserted.DeletedDate, 0), NULLIF(deleted.DeletedDate, 0))
, Core.insXMLFragment('ConfigID') + Core.addnlXMLFragment('ModifiedBy', inserted.ModifiedBy))
UPDATE Core.Config
SET Value = 'Insert'
, ModifiedBy = 'HARROGATE\Name'
, DeletedDate = NULL
WHERE ConfigID = '1'
AND Value = 'Update'
AND ModifiedBy = 'HARROGATE\Name'
AND NULLIF(DeletedDate, 0) IS NULL;

Related

Conversion failed Error SQL Date format Mismatch with C# Datatable datetime fromat depending on system calender setting

We have a Stored Procedure in SQL Server which expects datetime column to be in MMddyyyy format and results in error
Conversion failed when converting date and/or time from character string.
whenever the system calendar setting is set to ddMMyyyy format. The same SP works fine if other date format is set for the system such as dd-MMM-yyyy works fine and returns no error. SP throws error when the calender setting is changed to ddMMyyyy format. It will work fine for dates below 12 as it takes the first digits as month but fails when higher dates like 15 or 28 is given. I am trying to find a solution so that it will work for all calendar settings as I cannot restrict the end users from changing their calendar settings. Please help me ...
Here is my SP
CREATE PROCEDURE [dbo].[usp_704000]
-- Add the parameters for the stored procedure here
#Dt uttTrnBRS readonly,
#Dt1 uttTrnBRS1 readonly
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Declare #RowStatus Int
Declare #TrnId Int
Declare #DocNo Varchar(15)
Declare #NextNo Int
Declare #Return SmallInt
Select #RowStatus = MAX(RowStatus), #Return = 0, #TrnId = 0
From #Dt T0
BEGIN TRANSACTION
--NEW Mode Handle the Insertion of New Records
IF (#RowStatus = 2)
BEGIN
BEGIN TRY
Insert into TrnBRS (CmpId, FinYear, FYPrd, DocId, SeriesId, DocDate, DocRef, DocStatus, BankCode, BankName, BankAcNo, AcId, AcCode,
AcDesc, BankAcId, EndDt, EndBalAmt, LastRcnDt, BalAmt, Diff, Remarks, BranchCode, BaseId, CUserId, CreateDt)
Select T0.CmpId, T2.FinYear, T2.FYPrdId FYPrd, T3.DocId, T0.SeriesId, Cast(T0.DocDate as Date) DocDate, T0.DocRef, '3' DocStatus,
T0.BankCode, T0.BankName, T0.BankAcNo, T0.AcId, T0.AcCode, T0.AcDesc, T0.BankAcId,
Cast(T0.EndDt as Date) EndDt, ISNULL(T0.EndBalAmt, 0) EndBalAmt, Cast(T0.LastRcnDt as Date) LastRcnDt, ISNULL(T0.BalAmt, 0) BalAmt, ISNULL(T0.Diff, 0) Diff,
T0.Remarks, T3.BranchCode, T3.BaseId, T0.CUserId, GetDate() CreateDt
From #Dt T0 Cross Join CmpMst T1
Left Join FYPrdMst T2 ON T0.DocDate Between T2.PrdFrom and T2.PrdTo
Inner Join (Select T0.DocId, T0.BaseId, T1.SeriesId, T1.SFinYear, T1.EFinYear, T0.BranchCode
From DocNum T0 Inner Join DocNum1 T1 ON T0.DocId = T1.DocId
Where T0.BaseId = 34) T3 ON T2.FinYear Between T3.SFinYear and ISNULL(T3.EFinYear, 3000)
Where T0.RowStatus = 2;
IF (##ERROR = 0)
SET #TrnId = ##IDENTITY;
IF (##ERROR = 0)
Insert into TrnBRS1 (TrnId, LineNum, BranchCode, PDocNo, PDocDate, PDocRef, PRefDate, DrAmtLC, CrAmtLC, Narration, ClearAmt, BankDt, PTrnId, PBaseId, PDocId,
JnlBaseId, JnlDocId, JnlDocNo, JnlTrnId, JnlLineNum, AdjEntry, Notes, RowIndex)
Select #TrnId TrnId, ROW_NUMBER() OVER (Order by T0.RowIndex) LineNum, T0.BranchCode, T0.PDocNo, Cast(T0.PDocDate as Date) PDocDate, T0.PDocRef,
Cast(T0.PRefDate as Date) PRefDate, T0.DrAmtLC, T0.CrAmtLC, T0.Narration, T0.ClearAmt, Cast(T0.BankDt as Date) BankDt, T0.PTrnId, T0.PBaseId, T0.PDocId,
T0.JnlBaseId, T0.JnlDocId, T0.JnlDocNo, T0.JnlTrnId, T0.JnlLineNum, T0.AdjEntry, T0.Notes, ROW_NUMBER() OVER (Order by T0.RowIndex) RowIndex
From #Dt1 T0
Where ISNULL(T0.IsCheck, 'N') = 'Y'; -- and ISDATE(CONVERT(Varchar(10), T0.BankDt, 111)) = 1;
IF (##ERROR = 0)
Insert into TrnBRS2 (TrnId, LineNum, BranchCode, PDocNo, PDocDate, PDocRef, PRefDate, DrAmtLC, CrAmtLC, Narration, PTrnId, PBaseId, PDocId,
JnlBaseId, JnlDocId, JnlDocNo, JnlTrnId, JnlLineNum, Notes, RowIndex)
Select #TrnId TrnId, ROW_NUMBER() OVER (Order by T0.RowIndex) LineNum, T0.BranchCode, T0.PDocNo, T0.PDocDate, T0.PDocRef,
T0.PRefDate, T0.DrAmtLC, T0.CrAmtLC, T0.Narration, T0.PTrnId, T0.PBaseId, T0.PDocId,
T0.JnlBaseId, T0.JnlDocId, T0.JnlDocNo, T0.JnlTrnId, T0.JnlLineNum, T0.Notes, ROW_NUMBER() OVER (Order by T0.RowIndex) RowIndex
From #Dt1 T0
Where ISNULL(T0.IsCheck, 'N') = 'N'; -- and ISDATE(CONVERT(Varchar(10), T0.BankDt, 111)) = 1;
IF (##ERROR = 0)
Update TrnJnl1
SET RecnDt = T1.BankDt,
RecnSys = 'Y', RecnTrnId = #TrnId, RecnType = 1
From TrnJnl1 T0 Inner Join TrnBRS1 T1 ON T0.TrnId = T1.JnlTrnId and T0.LineNum = T1.JnlLineNum and T1.TrnId = #TrnId;
IF (##ERROR = 0)
Select #NextNo = T1.NextNo + 1,
#DocNo = ISNULL(T1.Prefix, '') + REPLICATE('0', T1.NumLen-LEN(CAST(#NextNo as VARCHAR(5))))+CAST(#NextNo as VARCHAR(5)) +
ISNULL(T1.Suffix, '')
From TrnBRS T0 Inner Join DocNum1 T1 ON T0.DocId = T1.DocId and T0.SeriesId = T1.SeriesId
Where T0.TrnId = #TrnId
IF (##ERROR = 0)
Insert into BankMst3 (BankAcId, BRDate, AcId, BRAmtLC, CUserId, CreateDate)
Select T0.BankAcId, T0.EndDt, T0.AcId, T0.EndBalAmt, T0.CUserId, GetDate() CreateDate
From TrnBRS T0
Where T0.TrnId = #TrnId;
IF (##ERROR = 0)
Update DocNum1
SET NextNo = #NextNo
From DocNum1 T0 Inner Join TrnBRS T1 ON T0.DocID = T1.DocID and T0.SeriesId = T1.SeriesId and T1.TrnId = #TrnId
IF (##ERROR = 0)
Update TrnBRS SET DocNo = #DocNo Where TrnId = #TrnId
IF (##ERROR = 0)
BEGIN
COMMIT TRANSACTION;
Select 0 RetVal, #TrnId TrnId, 'Transaction Saved...' ErrMsg, 0 ErrType;
RETURN
END
END TRY
BEGIN CATCH
BEGIN
ROLLBACK TRANSACTION;
SELECT -1 RetVal, #TrnId TrnId, ERROR_MESSAGE() AS ErrMsg, 1 ErrType
RETURN
END
END CATCH
END
ELSE
BEGIN
BEGIN TRY
Insert into TrnBRS (CmpId, FinYear, FYPrd, DocId, SeriesId, DocDate, DocRef, DocStatus, CancelState, CanclTrnId, EndDt, EndBalAmt,
LastRcnDt, BalAmt, Diff, Remarks, BaseId, CUserId, CreateDt)
Select T0.CmpId, T0.FinYear, T0.FYPrd, T0.DocId, T0.SeriesId, T0.DocDate, T0.DocRef, '3' DocStatus, 'N' CancelState, T0.TrnId CanclTrnId,
T0.EndDt, ISNULL(T0.EndBalAmt, 0) EndBalAmt, T0.LastRcnDt, ISNULL(T0.BalAmt, 0) BalAmt, ISNULL(T0.Diff, 0) Diff,
T0.Remarks, T0.BaseId, T0.CUserId, GetDate() CreateDt
From TrnBRS T0 Inner Join #Dt T1 ON T0.TrnId = T1.TrnId
Where T1.RowStatus = 3;
SET #TrnId = ##IDENTITY;
Update TrnBRS
SET CancelState = 'Y', DocStatus = '4', CanclTrnId = #TrnId
From TrnBRS T0 Inner Join #Dt T1 ON T0.TrnId = T1.TrnId and T1.RowStatus = 3;
Insert into TrnBRS1 (TrnId, LineNum, BranchCode, PDocNo, PDocDate, PDocRef, PRefDate, DrAmtLC, CrAmtLC, Narration, ClearAmt,
BankDt, PTrnId, PBaseId, PDocId, JnlBaseId, JnlDocId, JnlDocNo, JnlTrnId, JnlLineNum, AdjEntry, Notes, RowIndex)
Select #TrnId TrnId, T0.LineNum, T0.BranchCode, T0.PDocNo, T0.PDocDate, T0.PDocRef, T0.PRefDate, T0.DrAmtLC, T0.CrAmtLC,
T0.Narration, T0.ClearAmt *-1, T0.BankDt, T0.PTrnId, T0.PBaseId, T0.PDocId, T0.JnlBaseId, T0.JnlDocId, T0.JnlDocNo,
T0.JnlTrnId, T0.JnlLineNum, T0.AdjEntry, T0.Notes, T0.RowIndex
From TrnBRS1 T0 Inner Join #Dt T1 ON T0.TrnId = T1.TrnId and T1.RowStatus = 3;
Update TrnJnl1
SET RecnDt = NULL, RecnSys = NULL, RecnTrnId = NULL, RecnType = NULL
From TrnJnl1 T0 Inner Join TrnBRS1 T1 ON T0.TrnId = T1.JnlTrnId and T0.LineNum = T1.JnlLineNum and T1.TrnId = #TrnId
Select #NextNo = T1.NextNo + 1,
#DocNo = ISNULL(T1.Prefix, '') +
Substring(Cast(Year(T0.DocDate) as VARCHAR(4)),3,2) + REPLICATE('0', T1.NumLen-LEN(CAST(#NextNo as VARCHAR(5))))+CAST(#NextNo as VARCHAR(5)) +
ISNULL(T1.Suffix, '')
From #Dt T0 Inner Join DocNum1 T1 ON T0.DocId = T1.DocId
Update DocNum1
SET NextNo = #NextNo
From DocNum1 T0 Inner Join #Dt T1 ON T0.DocID = T1.DocID
IF (##ERROR <> 0) RETURN ##ERROR
Update TrnBRS SET DocNo = #DocNo Where TrnId = #TrnId
IF (##ERROR <> 0) RETURN ##ERROR
BEGIN
COMMIT TRANSACTION;
Select 0 RetVal, #TrnId TrnId, 'Transaction Reversed...' ErrMsg, 0 ErrType;
RETURN
END
END TRY
BEGIN CATCH
BEGIN
ROLLBACK TRANSACTION
SELECT -1 RetVal, #TrnId TrnId, ERROR_MESSAGE() AS ErrMsg, 1 ErrType
RETURN
END
END CATCH
END
END
I had tried to convert the ddMMyyyy format(103) using the convert statement SELECT CONVERT(DATE, T1.BankDt, 103) instead of T1.BankDt(which is the datetime field we are trying to manipulate) but didn't succeed. It still throws error saying conversion failed for dates above 12 as it expects dates in MMddyyyy format
EDIT 2
Thank You all for the supports...
I had find the issue at last, the real issue was in another stored procedure which is used to populate the initial value in which the BankDt field was populated with '' empty string so that the field is treated as character string instead of date field. I had changed '' BankDt to Convert(Date,null,103) BankDt ... Now everything works fine
the real issue was in another stored procedure which is used to populate the initial value in which the BankDt field was populated with '' empty string so that the field is treated as character string instead of date field. I had changed
'' BankDt
to
Convert(Date,null,103) BankDt
... Now everything works fine

Stored procedure update specific column by parameter

I am developing a stored procedure to update specific columns using parameters.
For example:
CREATE PROCEDURE save_proc
(
#userid varchar(5),
#column varchar(10),
#data varchar(50)
)
AS
BEGIN
UPDATE MyProfile
SET #column = #data
WHERE userid = #userid
The following is my code that I've tried but didn't work. Is it possible to do such an update by updating specific columns through parameters? Thanks
You can build up a query in your stored procedure and execute.
CREATE PROCEDURE save_proc
(
#userid varchar(5),
#column varchar(10),
#data varchar(50)
)
AS
BEGIN
exec sp_executesql N'UPDATE MyProfile SET ' + #column + '=''' + #data + ''' WHERE userid = ''' + #userid + ''''
END
The method may lead to security concern however.
Its doable but i would avoid doing it.... you code should be like below:
DECLARE #column varchar(10)
DECLARE #data varchar(50)
UPDATE DummyTable
SET
col1 = CASE WHEN #column = 'Col1' THEN #data ELSE col1 END ,
col2 = CASE WHEN #column = 'Col2' THEN #data ELSE col2 END
where userid = #userid
.
.
.
Hope that this is what you are looking for
Using a CASE statement causes all columns identified to once again be updated; this would invoke a reindexing for some indexes upon columns wherein that change, even if the same value, occurs. Waste of execution updates on server.
An IF statement is best option for single value, especially if value is NOT the same type as the column datatype.
IF(#fieldName IS NULL) SELECT #fieldName = ''
IF(#updateValue IS NULL) SELECT #updateValue = ''
DECLARE #myCount int
SELECT #myCount = count(*) FROM [dbo].[myTable] WHERE #inventoryID = #updateInventoryID
--if no valid data to update, return -1 for noUpdate
IF(0 = #updateInventoryID
OR 0 = #myCount
OR '' = #fieldName
OR '' = #updateValue)
RETURN -1
BEGIN TRAN
IF('notes' = #fieldName)
UPDATE [dbo].[myTable]
SET [notes] = #updateValue, [modifyDate] = GETDATE()
WHERE [inventoryID] = #updateInventoryID
IF('reorderPoint' = #fieldName)
BEGIN
SET #newValueInt = CONVERT(int, #updateValue)
UPDATE [dbo].[myTable]
SET [reorderPoint] = #newValueInt, [modifyDate] = GETDATE()
WHERE [inventoryID] = #updateInventoryID
END
IF('safetyStock' = #fieldName)
BEGIN
SET #newValueInt = CONVERT(int, #updateValue)
UPDATE [dbo].[myTable]
SET [safetyStock] = #newValueInt, [modifyDate] = GETDATE()
WHERE [inventoryID] = #updateInventoryID
END
IF('quantityOnHand' = #fieldName)
BEGIN
SET #newValueInt = CONVERT(int, #updateValue)
UPDATE [dbo].[myTable]
SET [quantityOnHand] = #newValueInt, [modifyDate] = GETDATE()
WHERE [inventoryID] = #updateInventoryID
END
IF('totalLeadTimeDays' = #fieldName)
BEGIN
SET #newValueInt = CONVERT(int, #updateValue)
UPDATE [dbo].[myTable]
SET [totalLeadTimeDays] = #newValueInt, [modifyDate] = GETDATE()
WHERE [inventoryID] = #updateInventoryID
END
IF('stockTakeETADate' = #fieldName)
BEGIN
SET #newValueDatetime = CONVERT(datetime, #updateValue)
UPDATE [dbo].[myTable]
SET [stockTakeETADate] = #newValueDatetime, [modifyDate] = GETDATE()
WHERE [inventoryID] = #updateInventoryID
END
COMMIT
A Try/Catch is advisable

Procedure to check annual date

I'm trying to create a procedure to verify annual dates. The date is stored one time in the BD, but in the calendar on my winform it shows annually(I use annually bolded dates in C#). So what I want to do with the query, is to check is the month and the day are like the date stored in the table, but does not work. This is my query:
SELECT IdCalendar,
Description,
DateCalendar,
Annualy
FROM Calendar
WHERE
(DATEPART(MONTH,DateCalendar) like DATEPART(MONTH,#DateCalendar)) AND
(DATEPART(DAY,DateCalendar) like DATEPART(DAY,#DateCalendar))
And for example, my stored DateCalendar is '2015-12-04', and I my paramenter #DateCalendar is '2016-12-04'. Any idea about how to do a better query?
EDIT
The query does not have any error or warning. Just returns 0 rows. And my DateCalendar is stored as DateTime.
The SP:
CREATE PROC [dbo].[usp_app_Calendar_Search]
#DateCalendar DATETIME,
#Result SMALLINT OUTPUT,
#Message VARCHAR(1000) OUTPUT
AS
BEGIN
DECLARE #vResult SMALLINT, #vMessage VARCHAR(1000)
SELECT #vResult = 0, #vMessage = ''
BEGIN TRY
IF EXISTS (SELECT * FROM Calendar WHERE DateCalendar = #DateCalendar)
BEGIN
IF(#DateCalendar = 0) SET #DateCalendar = NULL
SELECT IdCalendar,
Description,
DateCalendar,
Annualy
FROM Calendar
WHERE
(DATEPART(MONTH,DateCalendar) like DATEPART(MONTH,#DateCalendar)) AND
(DATEPART(DAY,DateCalendar) like DATEPART(DAY,#DateCalendar))
SET #vResult = 1
SET #vMessage = 'Done'
END
ELSE BEGIN
SET #vResult = 0
SET #vMessage = 'Error.'
END
END TRY
BEGIN CATCH
SET #vResult = -1
SET #vMessage = 'Error: ' + ERROR_MESSAGE() + ' Line: ' + CAST(ERROR_LINE() AS VARCHAR)
END CATCH
SELECT #Result = #vResult, #Message = #vMessage
END
Thanks in advance.
The problem is with IF EXISTS (SELECT * FROM Calendar WHERE DateCalendar = #DateCalendar). I rewrite your SP:
CREATE PROC [dbo].[usp_app_Calendar_Search]
#DateCalendar DATETIME,
#Result SMALLINT OUTPUT,
#Message VARCHAR(1000) OUTPUT
AS
BEGIN
DECLARE #vResult SMALLINT = 0
,#vMessage VARCHAR(1000) = '';
IF(#DateCalendar = 0) SET #DateCalendar = NULL;
BEGIN TRY
SELECT IdCalendar,
Description,
DateCalendar,
Annualy
FROM Calendar
WHERE DATEPART(MONTH,DateCalendar) = DATEPART(MONTH,#DateCalendar)
AND DATEPART(DAY,DateCalendar) = DATEPART(DAY,#DateCalendar);
IF ##ROWCOUNT > 0
SELECT #vResult = 1, #vMessage = 'Done'
ELSE
SELECT #vResult = 0, #vMessage = 'Error.';
END TRY
BEGIN CATCH
SET #vResult = -1
SET #vMessage = 'Error: ' + ERROR_MESSAGE() + ' Line: '
+ CAST(ERROR_LINE() AS VARCHAR)
END CATCH
SELECT #Result = #vResult, #Message = #vMessage;
END
EDIT:
Now the WHERE condition is not-SARGable so it means that query optimizer will skip index on DateCalendar column (if exists any).
You can use computed columns like #Tom Page suggested in comment:
ALTER TABLE Calendar ADD MonthCalendar AS DATEPART(MONTH,DateCalendar);
ALTER TABLE Calendar ADD DayCalendar AS DATEPART(day,DateCalendar);
/*Create Index on Calculated Columns for Month and day*/
CREATE INDEX IX_Calendar_Month_Day ON Calendar(MonthCalendar , DayCalendar);
/*Use Computed Column Index in W*/
DECLARE #DateCalendar datetime = '2015-12-25';
SELECT IdCalendar, Description, DateCalendar, Annualy
FROM Calendar
WHERE MonthCalendar = DATEPART(MONTH,#DateCalendar)
AND DayCalendar = DATEPART(DAY,#DateCalenda);

If Else not working in TSQL

If is never working. Always falling in else condition. I have debugged and checked #nextapprover is not null.
IF (#nextApprover != NULL)
BEGIN
UPDATE CallWatching
SET EmployeeNo = #employeeNo
WHERE #SRFID = MasterCode;
INSERT INTO CallForwarding (
MasterCode
,EmployeeNo
,ApproverNo
,IsForwarded
,ForwardBy
)
VALUES (
#SRFID
,#EmployeeNo
,#nextApprover
,0
,#EmployeeNo
)
END
ELSE
BEGIN
UPDATE CallWatching
SET STATUS = 1
WHERE #SRFID = MasterCode;
END
Use IS NOT NULL
i.e. Change: if(#nextApprover!=null)
to IF(#nextApprover IS NOT NULL)
Additionally you should <> instead of != as it is ANSI compliant

Update TableType variable in dynamic SQL query

I had created User defined table type in my database. After that I had declared a variable of that table type in my procedure. And I had return my rest of the logic. At the end I am trying to update that table type variable using dynamic SQL.
But I got an error:
Msg 10700, Level 16, State 1, Line 1
The table-valued parameter "#ttbl_TagList" is READONLY and cannot be modified.
How can I solve the error?
User defined table type:
CREATE TYPE [dbo].[tt_TagList] AS TABLE(
[tagListId] [tinyint] IDENTITY(1,1) NOT NULL,
[tagId] [tinyint] NOT NULL DEFAULT ((0)),
[tagName] [varchar](100) NOT NULL DEFAULT (''),
[tagValue] [nvarchar](max) NOT NULL DEFAULT ('')
)
GO
Procedure:
CREATE PROCEDURE [dbo].[P_ReplaceTemplateTag]
AS
BEGIN
SET NOCOUNT ON;
DECLARE #ttbl_TagList dbo.tt_TagList, #V_TagId INT, #V_Counter TINYINT = 1,
#V_FinalQuery NVARCHAR(MAX) = '', #V_TagValue NVARCHAR(MAX);
INSERT INTO #ttbl_TagList (tagId, tagName, tagValue)
SELECT DISTINCT T.tagId, T.tagName, '' AS tagValue
FROM dbo.tbl_Tag T;
WHILE (1 = 1)
BEGIN
SET #V_TagValue = '';
SELECT #V_TagId = tagId FROM #ttbl_TagList WHERE tagListId = #V_Counter;
IF (##ROWCOUNT = 0)
BEGIN
BREAK;
END
IF (#V_TagId = 1)
BEGIN
/* Logic for getting tag value */
SET #V_TagValue = 'Tag Value 1';
END
ELSE IF (#V_TagId = 2)
BEGIN
/* Logic for getting tag value */
SET #V_TagValue = 'Tag Value 2';
END
ELSE IF (#V_TagId = 3)
BEGIN
/* Logic for getting tag value */
SET #V_TagValue = 'Tag Value 3';
END
ELSE IF (#V_TagId = 4)
BEGIN
/* Logic for getting tag value */
SET #V_TagValue = 'Tag Value 4';
END
IF (#V_TagValue != '')
BEGIN
SET #V_FinalQuery = #V_FinalQuery + ' WHEN ' + CONVERT(NVARCHAR(10), #V_Counter) + ' THEN ''' + #V_TagValue + '''';
END
SET #V_Counter = #V_Counter + 1;
END
IF (#V_FinalQuery != '')
BEGIN
SET #V_FinalQuery = N'UPDATE #ttbl_TagList SET tagValue = (CASE tagListId' + #V_FinalQuery + ' END)';
EXECUTE sp_executesql #V_FinalQuery, N'#ttbl_TagList dbo.tt_TagList readonly', #ttbl_TagList;
END
SELECT * FROM #ttbl_TagList;
END
TVP can not be updated directly. So try this one -
CREATE PROCEDURE [dbo].[P_ReplaceTemplateTag]
AS
BEGIN
SET NOCOUNT ON;
IF OBJECT_ID('tempdb.dbo.#t1') IS NOT NULL
DROP TABLE #t1
DECLARE
#ttbl_TagList dbo.tt_TagList
, #V_Counter TINYINT = 1
, #V_FinalQuery NVARCHAR(MAX) = ''
, #V_TagValue NVARCHAR(MAX);
INSERT INTO #ttbl_TagList (tagId, tagName, tagValue)
SELECT DISTINCT tagId, tagName, ''
FROM dbo.tbl_Tag;
WHILE (1 = 1) BEGIN
SELECT #V_TagValue =
CASE
WHEN tagId = 1 THEN 'Tag Value 1'
WHEN tagId = 2 THEN 'Tag Value 2'
WHEN tagId = 3 THEN 'Tag Value 3'
WHEN tagId = 4 THEN 'Tag Value 4'
ELSE ''
END
FROM #ttbl_TagList
WHERE tagListId = #V_Counter;
IF (##ROWCOUNT = 0) BREAK;
IF (#V_TagValue != '')
BEGIN
SET #V_FinalQuery = #V_FinalQuery + ' WHEN ' + CONVERT(NVARCHAR(10), #V_Counter) + ' THEN ''' + #V_TagValue + '''';
END
SET #V_Counter = #V_Counter + 1;
END
IF (#V_FinalQuery != '')
BEGIN
CREATE TABLE #t1
(
tagId TINYINT
, tagName VARCHAR(100)
, tagValue NVARCHAR(MAX)
)
SET #V_FinalQuery = N'
INSERT INTO #t1
SELECT tagId, tagName, (CASE tagListId' + #V_FinalQuery + ' END)
FROM #ttbl_TagList';
EXEC sys.sp_executesql
#V_FinalQuery
, N'#ttbl_TagList dbo.tt_TagList readonly'
, #ttbl_TagList;
END
SELECT * FROM #t1;
END