AmpScript issue with IF Statement - ampscript

I am trying to do a lookup to two different data extensions in exact target using ampscript. Please find the sample code I am trying .
%%[
Var #rows
Set #rows=LookupRows("DataExtensionOne","Lead Owner","Nick")
FOR #y = 1 TO RowCount(#rows) DO
Set #currentRow = Row(#rows,#y)
Set #value = FIELD(#currentRow ,"LeadId")
Set #secondDERows = LookupRows("DataExtensionTwo","Gender","Male")
FOR #x = 1 TO RowCount(#secondDERows) DO
Set #currentRowInSecDE = Row(#secondDERows,#x)
Set #secValue = FIELD(#currentRowInSecDE ,"LeadId")
IF #value == #secValue THEN
Set #FirstName = FIELD(#currentRowInSecDE ,"FirstName")
/* Need to break out of the loop */
]%%
The If condition check seems to fail #value == #secValue .
It doesnt fetch any value for the #FirstName. And what statement should be used to break out of IF loop ?
Has anyone come across similar problems ? Please do let me know.

As far as I am aware, ampscript does not have a break operator.
In this case, I would set up a boolean value that is checked to be false at the start of each loop and is set to True when you find a match. This way once you have matched you'll still run through the rest of the loop but noting will happen within them.
%%[
Var #rows
Set #found_result = False
Set #rows=LookupRows("DataExtensionOne","Lead Owner","Nick")
FOR #y = 1 TO RowCount(#rows) DO
if not #found_result then
Set #currentRow = Row(#rows,#y)
Set #value = FIELD(#currentRow ,"LeadId")
Set #secondDERows = LookupRows("DataExtensionTwo","Gender","Male")
FOR #x = 1 TO RowCount(#secondDERows) DO
if not #found_result then
Set #currentRowInSecDE = Row(#secondDERows,#x)
Set #secValue = FIELD(#currentRowInSecDE ,"LeadId")
IF #value == #secValue THEN
Set #FirstName = FIELD(#currentRowInSecDE ,"FirstName")
Set #found_result = True
ENDIF
endif
NEXT #x
endif
NEXT #y
]%%

%%[
Var #rows
Set #found_result = False
Set #rows=LookupRows("DataExtensionOne","Lead Owner","Nick")
FOR #y = 1 TO RowCount(#rows) DO
if #found_result == "TRUE" then
Set #currentRow = Row(#rows,#y)
Set #value = FIELD(#currentRow ,"LeadId")
Set #secondDERows = LookupRows("DataExtensionTwo","Gender","Male")
FOR #x = 1 TO RowCount(#secondDERows) DO
if #found_result == "TRUE" then
Set #currentRowInSecDE = Row(#secondDERows,#x)
Set #secValue = FIELD(#currentRowInSecDE ,"LeadId")
IF #value == #secValue THEN
Set #FirstName = FIELD(#currentRowInSecDE ,"FirstName")
Set #found_result = True
ENDIF
endif
NEXT #x
endif
NEXT #y
]%%

Related

Function to check the input number of words

Need help to create a function that returns TRUE or FALSE. TRUE - if type 1 or 3 words (like '__hello_', '_hello', '_hello my frend' - spaces should be cut), if the condition is not fulfilled FALSE
CREATE FUNCTION dbo.nazvFac(#f nvarchar(30))
RETURNS bit
AS
BEGIN
DECLARE #l int = 1, #s nvarchar(30), #i int = 0, #b bit
WHILE LTRIM(RTRIM(LEN(#f))) >= #l --error here, but I do not know how to fix it
BEGIN
SET #s = SUBSTRING(#f, #l, 1)
IF #s BETWEEN 'А' AND 'я'
SET #l += 1
ELSE IF #s = ' '
BEGIN
SET #l -= 1
SET #s = SUBSTRING(#f, #l, 1)
SET #s = RTRIM(#s)
SET #l += 2
SET #i += 1
END
ELSE
BREAK
END
IF #i = 0 OR #i = 2
SET #b = 'TRUE'
ELSE
SET #b = 'FALSE'
RETURN #b
END
GO
WHILE LTRIM(RTRIM(LEN(#f))) >= #l --error here, but I do not know how to fix it
LEN() returns an int, which you are then passing to a string function: RTRIM().
You want to return TRUE only if there are one or three words? This should do it:
CREATE FUNCTION dbo.nazvFac(#f NVARCHAR(30))
RETURNS BIT
AS
BEGIN
DECLARE #l INT, #b BIT
SET #l = LEN(#f) - LEN(REPLACE(#f, ' ', '')) + 1
IF #l == 1 OR #l == 3
SET #b = 'TRUE'
ELSE
SET #b = 'FALSE'
RETURN #b
END
Also, JC. is right about the len() error.
You should trim the string and then check the length.
CREATE FUNCTION dbo.nazvFac(#f NVARCHAR(30))
RETURNS BIT
AS
BEGIN
DECLARE #l INT = 1, #s NVARCHAR(30), #i INT = 0, #b BIT
WHILE LEN(LTRIM(RTRIM(#f))) >= #l --I think youn need to trim the string and then check length
BEGIN
SET #s = SUBSTRING(#f, #l, 1)
IF #s BETWEEN 'А' AND 'я'
SET #l += 1
ELSE IF #s = ' '
BEGIN
SET #l -= 1
SET #s = SUBSTRING(#f, #l, 1)
SET #s = RTRIM(#s)
SET #l += 2
SET #i += 1
END
ELSE
BREAK
END
IF #i = 0 OR #i = 2
SET #b = 'TRUE'
ELSE
SET #b = 'FALSE'
RETURN #b
END
GO

My SQL Function is not returning the value I expected

This was my first attempt at a SQL function. I wrote it in VB and it works like a charm. When I translated it to SQL Server, it returns not what I expect. What the function is intended to do is to return a percentage match of two strings.
How I expected it to function is this:
accept two strings, compare the two, and based on my rating values, return a percentage of the match value...matchscore/max possiblescore
The length of the larger string is multiplied by 3. This is the max possiblescore.
Go character by character of the first string and find that character in the second string.
If the character is found in the same position in the second string, add three to the matchscore and move on to the next letter.
If the character is found in the second word, but not in the same position, add one to match score and move on to the next character.
If the character is not found in the second string, add nothing and move on to the next character.
Divide the matchscore by the max possiblescore. This returns a decimal value. I read RETURN only returns an integer, so I multiplied the division result by 100.
An example of what I expected is I compare "CAT" to "CART". My expected return is 7/12...0.58. Instead, I get 0. If I compare "CAT" to "CAT", I expect 9/9...1.00. Instead, I get 2.
(Note from 9/17/2014: I appreciate your input. I used what you suggested, and did one more major change, that doesn't affect what I asked about, other than getting the correct final answer is that I got rid of the second While Loop. Instead, I search for #strLetter in #strWord2. If it is found, then, I look to see if it is in the same position in #strWord2 as #strWord1. If it is, then I add 3, if not, I add 1. This sped up the function and made the count accurate.
Here is the code:
CREATE FUNCTION [dbo].[CompareWords]
(#strWord1 VARCHAR(2000), #strWord2 VARCHAR(2000))
RETURNS DECIMAL
AS
BEGIN
SET #strWord1 = UPPER(#strWord1)
SET #strWord2 = UPPER(#strWord2)
DECLARE #intLength INT
IF LEN(#strWord1) >= LEN(#strWord2)
BEGIN
SET #intLength = LEN(#strWord1)
END
ELSE
BEGIN
SET #intLength = LEN(#strWord2)
END
DECLARE #iWordLoop1 INT
DECLARE #iWordLoop2 INT
DECLARE #intWordLoop2 INT
DECLARE #intWordScore INT
DECLARE #intLetterScore INT
SET #intWordScore = 0
SET #intWordLoop2 = Len(#strWord2)
DECLARE #strLetter VARCHAR(1000)
DECLARE #count1 INT
SET #count1 = 0
SET #iWordLoop1 = Len(#strWord1)
WHILE (#count1 < #iWordLoop1)
BEGIN
SET #strLetter = SUBSTRING(#strWord1, #count1+1, 1)
SET #intLetterScore = 0
DECLARE #count2 INT
SET #count2 = 0
SET #iWordLoop2 = Len(#strWord2)
WHILE (#count2 < #iWordLoop2)
BEGIN
If #strLetter = SUBSTRING(#strWord2, #count2+1, 1)
BEGIN
If #iWordLoop1 = #iWordLoop2
BEGIN
SET #intLetterScore = 3
SET #iWordLoop2 = Len(#strWord2)
END
ELSE
BEGIN
SET #intLetterScore = 1
END
END
SET #intWordScore = #intWordScore + #intLetterScore
SET #count2 = (#count2 + 1)
END
SET #count1 = (#count1 + 1)
END
DECLARE #sinScore DEC
SET #sinScore = (#intWordScore / (3 * #intLength)) * 100
RETURN #sinSCore
END;
The most significant changes I made were to
reset the intLetterScore to 0 after it's been used in the intWordScore calculation. Without it being reset, the same value was being used each time the inner loop and the character was not matched.
move the multiplication by 100 into the
brackets in the calculation of sinScore.
As referred to in a previous post, because you are doing integer multiplication, the decimal portion is truncated from the calculation. By growing the wordScore by a factor of 100, it is much more likely to be larger than the length and yield a result which is non-zero.
Multiplying outside the brackets has the multiplies the integer result of the division score by length. If this answer is already zero, then the multiplication result is also zero.
Other changes I made are commented in the code: the variable intWordLoop2 has no effect on the calculation and can be removed; strLetter can be declared as a Char(1) instead of VarChar(1000).
CREATE FUNCTION [dbo].[CompareWords]
(#strWord1 VARCHAR(2000), #strWord2 VARCHAR(2000))
RETURNS DECIMAL
AS
BEGIN
SET #strWord1 = UPPER(#strWord1)
SET #strWord2 = UPPER(#strWord2)
--Set #intLength (maxLength as len of word1 or word2)
DECLARE #intLength INT --maxLength
IF LEN(#strWord1) >= LEN(#strWord2)
BEGIN
SET #intLength = LEN(#strWord1)
END
ELSE
BEGIN
SET #intLength = LEN(#strWord2)
END
DECLARE #iWordLoop1 INT, #iWordLoop2 INT--, #intWordLoop2 INT --This variable doesn't impact the calculation
DECLARE #intWordScore INT
DECLARE #intLetterScore INT
SET #intWordScore = 0
--SET #intWordLoop2 = Len(#strWord2)--this value is not used anywhere else, so removing makes no difference.
--DECLARE #strLetter VARCHAR(1000)
DECLARE #strLetter CHAR(1)--there is no need for 1000 characters since we're only ever assigning a single character to this
DECLARE #count1 INT
SET #count1 = 0
SET #iWordLoop1 = Len(#strWord1)
WHILE (#count1 < #iWordLoop1)
BEGIN
SET #strLetter = SUBSTRING(#strWord1, #count1+1, 1)
SET #intLetterScore = 0
DECLARE #count2 INT
SET #count2 = 0
SET #iWordLoop2 = Len(#strWord2)
WHILE (#count2 < #iWordLoop2)
BEGIN
If #strLetter = SUBSTRING(#strWord2, #count2+1, 1)
BEGIN
If #iWordLoop1 = #iWordLoop2
BEGIN
SET #intLetterScore = 3
SET #iWordLoop2 = Len(#strWord2)
END
ELSE
BEGIN
SET #intLetterScore = 1
END
END
SET #intWordScore = #intWordScore + #intLetterScore
SET #intLetterScore = 0
SET #count2 = (#count2 + 1)
END
SET #count1 = (#count1 + 1)
END
DECLARE #sinScore DEC
SET #sinScore = (#intWordScore*100 / (3 * #intLength))
RETURN #sinSCore
END;
select dbo.comparewords ('Cat','cart')

Invalid length parameter passed to the LEFT or SUBSTRING function

Following is my query and it works fine when I have like TRAILER_MAKE_MODEL 'testing ~ testing, test ~ testq,' ends with comma but can't handle 'testing ~ testing, test ~ testq' same as for other variable TRAILER_IDV. I tried my best but cant work it out any help would be appreciated.
My aim is to get the comma separated value for xml.
DECLARE #TRAILER_MAKE_MODEL VARCHAR(MAX)
DECLARE #TRAILER_IDV VARCHAR(MAX)
DECLARE #USER_TYPE VARCHAR(MAX)
DECLARE #START_INDEX_1 INT
DECLARE #START_INDEX_4 INT
DECLARE #END_INDEX_1 INT
DECLARE #END_INDEX_4 INT
DECLARE #VALUE VARCHAR(50)
DECLARE #QUERY VARCHAR(MAX)
set #TRAILER_MAKE_MODEL='testing ~ testing, test ~ testq,'
set #TRAILER_IDV='3500, 3400,'
set #USER_TYPE='MOBILE'
set #QUERY = ''
set #START_INDEX_1 = 1
set #START_INDEX_4 = 1
set #END_INDEX_1 = 0
if ISNULL(#TRAILER_MAKE_MODEL,'') <> ''
begin
WHILE #START_INDEX_1 > 0 and #START_INDEX_1 < len(#TRAILER_MAKE_MODEL)
BEGIN
SET #END_INDEX_1 = CHARINDEX(',',#TRAILER_MAKE_MODEL,#START_INDEX_1)
if #END_INDEX_1 = 0 and #START_INDEX_1 = 1
Begin
SET #END_INDEX_1 = len(#TRAILER_MAKE_MODEL)
END
if #USER_TYPE <> 'MOBILE'
Begin
SET #END_INDEX_1 = #END_INDEX_1 +1
End
SET #VALUE = SUBSTRING(#TRAILER_MAKE_MODEL,#START_INDEX_1,#END_INDEX_1 - #START_INDEX_1)
SET #QUERY = #QUERY + 'UNION ALL SELECT ''' + #VALUE + ''' TRAILER_MAKE_MODEL'
SET #END_INDEX_4 = CHARINDEX(',',#TRAILER_IDV,#START_INDEX_4)
if #END_INDEX_4 = 0 and #START_INDEX_4 = 1
Begin
SET #END_INDEX_4 = len(#TRAILER_IDV)
END
if #USER_TYPE <> 'MOBILE'
Begin
SET #END_INDEX_4 = #END_INDEX_4 +1
End
SET #VALUE = SUBSTRING(#TRAILER_IDV,#START_INDEX_4,#END_INDEX_4 - #START_INDEX_4)
SET #QUERY = #QUERY + ',' + #VALUE + 'TRAILER_IDV '
print #QUERY
SET #START_INDEX_1 = #END_INDEX_1 + 1
SET #START_INDEX_4 = #END_INDEX_4 + 1
END
select #QUERY=substring(#QUERY, 10, LEN(#QUERY) - 9)
EXEC (#QUERY)
END
You already have a lot of code here so two extra lines where you assign a comma at the end of each string should probably not slow things down for you or make the code less maintainable.
SET #TRAILER_MAKE_MODEL += ',';
SET #TRAILER_IDV += ',';
I don't really understand what your code does but to get the result you are getting you can use a split string function that returns the index of the item like this.
select T1.Item as TRAILER_MAKE_MODEL,
T2.Item as TRAILER_IDV
from dbo.SplitString(#TRAILER_MAKE_MODEL, ',') as T1
inner join dbo.SplitString(#TRAILER_IDV, ',') as T2
on T1.ItemNumber = T2.ItemNumber

Selecting Multiple Substrings from a Field

I'm trying to create a SQL query that selects pieces of a record from a field. Here is a shorten example of what is in one unedited field:
<Name>Example1</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Start Date (DD-MMM-YYYY)</Prompt>
<PromptUser>True</PromptUser> </Parameter>
<Parameter>
<Name>Example2</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Case (Enter Case Number, % for all, OR %AL% for Alberta)</Prompt>
<PromptUser>True</PromptUser>
<DefaultValues>
<Value>%al%</Value>
</DefaultValues>
<Values>
<Value>%al%</Value>
</Values> </Parameter>
<Parameter>
A utter messy right, well I'm trying pull out all names, prompts and if it has a value then its value and put all of that into one field formated. For example the above field should look like this
Name: Example1
Prompt: Start Date (DD-MMM-YYYY)
Name: Example2
Prompt: Case (Enter Case Number, % for all, OR %AL% for Alberta)
Value: %al%
I've tried using STUFF but there can be any number Names with Prompts and values in a single field. My next thought was to use replace to replace all the <> and but that would leave me with the stuff inbetween like so
Name: Example1
String
False
False
Prompt: Start Date (DD-MMM-YYYY)
Name: Example2
String
False
False
Prompt: Case (Enter Case Number, % for all, OR %AL% for Alberta)
True
Value: %al%
%al%
Edit: Another idea that might solve the problem is if I can use REPLACE to replace the unknown length string between or along with the two known characters/strings for example replacing <Type>###</Type> where ### represents any number of characters inbetween the two known strings and . The problem is that I don't know if this is even possible or how to do it if it is.
Any suggestions are apperciated.
so I checked the code with management studio and discover a few errors.
declare #var nvarchar(max)
declare #tag nvarchar(max)
declare #label nvarchar(max)
declare #start int
declare #stop int
declare #len int
declare #needed int
set #var = '<Name>Example1</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Start Date (DD-MMM-YYYY)</Prompt>
<PromptUser>True</PromptUser>
<Parameter> </Parameter>
<Name>Example2</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Case (Enter Case Number, % for all, OR %AL% for Alberta)</Prompt>
<PromptUser>True</PromptUser>
<DefaultValues>
<Value>%al%</Value>
</DefaultValues>
<Values>
<Value>%al%</Value>
</Values>
<Parameter></Parameter>'
set #needed = 0
set #start = charindex('<',#var)
set #stop = charindex('>',#var)
set #len = #stop - #start +1
set #tag = substring(#var,#start,#len)
set #label = substring(#var,#start+1,#len-2)
set #var = replace(#var,#tag,#label + ' : ')
while(#start <> 0)
begin
set #start = charindex('<',#var)
set #stop = charindex('>',#var)
set #len = #stop - #start +1
if(#start <> 0)
begin
set #tag = substring(#var,#start,#len)
if(charindex('/',#tag) = 0)
begin
set #label = substring(#var,#start+1,#len-2)+ ' : '
if(lower(#label) <> 'name : ' and lower(#label) <> 'value : ' and lower(#label) <> 'prompt : ')
begin
set #needed = 0
set #var = replace(#var,#tag,'')
set #start = #stop - len(#tag)
set #stop = charindex('<',#var)
set #len = #stop - #start
set #tag = substring(#var,#start,#len)
set #var = replace(#var,#tag,'')
end
end
else
begin
set #label = ''
end
set #var = replace(#var,#tag,#label)
end
end
print replace(#var,'
','')
and this results in:
Name : Example1
Prompt : Start Date (DD-MMM-YYYY)
Name : Example2
Prompt : Case (Enter Case Number, % for all, OR %AL% for Alberta) Value :
%al%
this is what I came up with. I hope it helps. basically it looks for the tags, if it's a closing tag it removes it and if it's an open tag it is replaces by the label variant.
declare #var nvarchar(max)
declare #tag nvarchar(max)
declare #label nvarchar(max)
declare #start int
declare #stop int
declare #len int
set #var = '<Name>Example1</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Start Date (DD-MMM-YYYY)</Prompt>
<PromptUser>True</PromptUser>
<Parameter> </Parameter>
<Name>Example2</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Case (Enter Case Number, % for all, OR %AL% for Alberta)</Prompt>
<PromptUser>True</PromptUser>
<DefaultValues>
<Value>%al%</Value>
</DefaultValues>
<Values>
<Value>%al%</Value>
</Values>
<Parameter></Parameter>'
set #start = charindex('<',#var)
set #stop = charindex('>',#var)
set #len = #stop - #start +1
set #tag = substring(#var,#start,#len)
set #label = substring(#var,#start+1,#len-2)
set #var = replace(#var,#tag,#label + ' : ')
while(#start <> 0)
begin
set #start = charindex('<',#var)
set #stop = charindex('>',#var)
set #len = #stop - #start +1
if(#start <> 0)
begin
set #tag = substring(#var,#start,#len)
if(charindex('/',#tag) = 0)
begin
set #label = substring(#var,#start+1,#len-2)+ ' : '
end
else
begin
set #label = ''
end
set #var = replace(#var,#tag,#label)
end
end
print #var
this results in:
Name : Example1
Type : String
Nullable : False
AllowBlank : False
Prompt : Start Date (DD-MMM-YYYY)
PromptUser : True
Parameter :
Name : Example2
Type : String
Nullable : False
AllowBlank : False
Prompt : Case (Enter Case Number, % for all, OR %AL% for Alberta)
PromptUser : True
DefaultValues :
Value : %al%
Values :
Value : %al%
Parameter :
I changed the code so that only the name, prompt and value parts will be saved.
A little warning though I changed the code in notepad++ so it could have a bug.
the idea is that you put this code in a function and pass the #var part as a parameter.
declare #var nvarchar(max)
declare #tag nvarchar(max)
declare #label nvarchar(max)
declare #start int
declare #stop int
declare #len int
declare #needed int
set #var = '<Name>Example1</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Start Date (DD-MMM-YYYY)</Prompt>
<PromptUser>True</PromptUser>
<Parameter> </Parameter>
<Name>Example2</Name>
<Type>String</Type>
<Nullable>False</Nullable>
<AllowBlank>False</AllowBlank>
<Prompt>Case (Enter Case Number, % for all, OR %AL% for Alberta)</Prompt>
<PromptUser>True</PromptUser>
<DefaultValues>
<Value>%al%</Value>
</DefaultValues>
<Values>
<Value>%al%</Value>
</Values>
<Parameter></Parameter>'
set #start = charindex('<',#var)
set #stop = charindex('>',#var)
set #len = #stop - #start +1
set #tag = substring(#var,#start,#len)
set #label = substring(#var,#start+1,#len-2)
set #var = replace(#var,#tag,#label + ' : ')
while(#start <> 0)
begin
set #start = charindex('<',#var)
set #stop = charindex('>',#var)
set #len = #stop - #start +1
if(#start <> 0)
begin
set #tag = substring(#var,#start,#len)
if(charindex('/',#tag) = 0)
begin
set #label = substring(#var,#start+1,#len-2)+ ' : '
if(lower(#label) = 'name' or lower(#label) = 'value' or lower(#label) = 'prompt')
begin
set #needed = 1
end
else
begin
set #needed = 0
end
end
else
begin
set #label = ''
end
if(#needed = 1)
begin
set #var = replace(#var,#tag,#label)
end
else
begin
set #var = replace(#var,#tag,'')
set #start = #stop
set #stop = charindex('<',#var)
set #tag = substring(#var,#start,#stop)
set #var = replace(#var,#tag,'')
end
end
end
print #var

Checking if every parameter of a stored procedure got default value or not

How would you implement that check? You have N parameters for a stored procedure.
If all value are null or 0 or empty string, run the code else skip it
This is how I implemented it, is there a better way?
I really don't like what I did so I'm open to any better idea, must be easily maintainable since it would be done at many places
declare #doRunIt bit
declare #checkAllNull varchar(max)
declare #sumOfInt int
set #checkAllNull = ''
set #checkAllNull = #checkAllNull + coalesce(#param1,'')
set #checkAllNull = #checkAllNull + coalesce(#param2,'')
set #checkAllNull = #checkAllNull + coalesce(#param3,'')
set #checkAllNull = #checkAllNull + coalesce(#param4,'')
set #checkAllNull = #checkAllNull + coalesce(#param5,'')
set #checkAllNull = #checkAllNull + coalesce(#param6,'')
set #sumOfInt = coalesce(#param7,0)+coalesce(#param8,0)+
coalesce(#param9,0)+coalesce(#param10,0)+
coalesce(#param11,0)+coalesce(#param12,0)
set #checkAllNull = #checkAllNull + cast(#sumOfInt as varchar(max))
if ( isnumeric(#checkAllNull)=1 )
if (cast(#checkAllNull as int) > 0)
set #doRunIt = 1
else
set #doRunIt = 0
else
if (ltrim(rtrim(#checkAllNull)) <> '')
set #doRunIt = 1
else
set #doRunIt = 0
End goal is to move the check made in code for inserting empty row, if all parameters are null or using default value, in the DB into the stored procedure, so other apps can call the same stored procedure without having to deal with checking for empty row.
Your code
DECLARE #IsValid BIT = dbo.IsValidParameter(#param1) &
dbo.IsValidParameter(#param2) &
dbo.IsValidParameter(#param3) &
dbo.IsValidParameter(#param4)
Helper function
CREATE FUNCTION dbo.IsValidParameter(#p1 Sql_Variant)
RETURNS bit
AS
BEGIN
IF #p1 IS NULL OR #p1 = '' OR #p1 = 0
RETURN 0
RETURN 1
END
GO