I'm trying to use IIF for the first time instead of a CASE and SQL Server is throwing this error:
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '>'.
My code:
DECLARE #lDate date = CONVERT(date,GETDATE());
DECLARE #lMonth int = MONTH(#lDate);
DECLARE #lDay int = DAY(#lDate);
DECLARE #lPeriodStart date, #lPeriodEnd date, #lPayPeriod int, #lCutOffDay int = 14;
--there are 24 pay periods find which one
SET #lPayPeriod = #lMonth * 2 - IIF(#lDay > #lCutOffDay, 0, 1);
SELECT #lPayPeriod
I don't understand how this is different from the guidelines from MSDN.
IIF was introduced in SQL Server 2012. As you've tagged your question with SQL Server 2008 I'd say that is the source of the problem.
Your syntax is valid and the code would work in a newer version.
Related
I wrote a script which check referencing objects on SQL Server versions from 2008 to 2016.
In SQL Server 2008, RaiseError works fine, but in SQL Server 2016, when I use TRY-CATCH, I don't see any errors in messages even they occur.
How to do it, to work also on 2016 version and be compatible up to the SQL Server 2008 version?
BEGIN TRY
INSERT INTO #refTable
SELECT
#nestLevel,
#referencingEntityFullName AS referencing_object_name,
OBJECT_ID(#referencingEntityFullName) AS referencing_object_id,
r.referencing_minor_id, --if > 0 then computed column
r.referenced_server_name,
r.referenced_database_name,
r.referenced_schema_name,
r.referenced_entity_name,
r.referenced_minor_name,
r.is_caller_dependent, --warning on these!
r.is_ambiguous--,
--r.is_selected,
--r.is_updated,
--r.is_select_all,
--r.is_insert_all,
--r.is_all_columns_found
FROM
sys.dm_sql_referenced_entities(#referencingEntityFullName, 'OBJECT') r
WHERE
r.referenced_entity_name = #currentReferencedEntityName
AND (#filterColumns = 0 OR
r.referenced_minor_name IN (SELECT ColumnName
FROM #piiTablesAndColumns ptac
WHERE ptac.TableName = #currentReferencedEntityFullName)
OR r.referenced_minor_name IS NULL --for SELECT *; check if it really works this way
)
END TRY
BEGIN CATCH
print('catched!' + #referencingEntityFullName + CAST(OBJECT_ID(#referencingEntityFullName) AS NVARCHAR(1000)))
INSERT INTO #refTable (referencing_object_name, referencing_object_id, referenced_entity_name)
VALUES (#referencingEntityFullName, OBJECT_ID(#referencingEntityFullName), (SELECT top 1 referenced_entity_name FROM sys.sql_expression_dependencies WHERE referencing_id=OBJECT_ID(#referencingEntityFullName) and referenced_entity_name in (select replace(TableName,'dbo.','') from #piiTablesAndColumnsTmp)))
RAISERROR('error',255,255)
END CATCH
Current Date Format: 12-18-2018-03:14:48
I want to convert to: 2018-12-18 03:14
Currently using SQL Server 2008
I'm using this code syntax:
DECLARE #input VARCHAR(35) = '12-18-2018-03:14:48'
SELECT CONVERT(DATETIME, #input, 120)
Error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Please help. Thank you!
I'm not sure that your current datetime literal falls into any mask which SQL Server can recognize. But we can try using TRY_CONVERT here, replacing the middle dash with a space:
SELECT TRY_CONVERT(datetime, STUFF(#input, 11, 1, ' ')) AS output;
18/12/2018 03:14:48
Demo
Edit:
If you are using an earlier version of SQL Server which does not support TRY_CONVERT, then we can try explicitly using CONVERT:
SELECT CONVERT(datetime, STUFF(#input, 11, 1, ' ')) AS output;
I'm working with SQL Server 2008 R2 and I have an stored procedure where I'm trying to throw errror
throw 50001, 'Couldnot process,Please verify Transaction ID and EmbossLine', 1
But the query doesn't get executed and throws error on 50001. It was working fine on SQL Server 2012. I think there is some issue with versions. Is there anyother way I can throw error in SQL Server 2008 R2?
This is my stored procedure:
Alter procedure [dbo].[spGetRedemption]
#pan varchar(19),
#transId bigint
AS
Begin
if EXISTS(select * from POS_Transactions where ID=#transId)
Begin
select
PT.ID, PT.TransactionDate, M.MerchantName1,
PT.TerminalID, PT.BatchNumber, PT.SequenceNumber, PT.PAN,
C.EmbossName, PT.TotalAmount, PT.CurrencyCode,
TT.TransactionType, PT.InvoiceNumber
from
POS_Transactions PT
inner join
Terminal T on T.TerminalID = PT.TerminalID
inner join
Merchant M on M.MerchantID = T.MerchantID
inner join
Card C on C.EmbossLine = PT.PAN
inner join
TransactionType TT on TT.TransactionTypeID = PT.TransactionTypeID
where
PT.ID = #transId
and PT.PAN = #pan
END
Else
Begin
throw 50001, 'Couldnot process,Please verify Transaction ID and EmbossLine', 1
END
End
Throw is not available in SQL Server 2008R2; it was first introduced in SQL Server 2012.
https://msdn.microsoft.com/en-us/library/ee677615(v=sql.110).aspx
The alternative would be to use Raiserror (note; only 1 E in the middle; it's not RaiseError).
From the above link, there are some differences between these methods:
RAISERROR statement
If a msg_id is passed to RAISERROR, the ID must be defined in sys.messages.
The msg_str parameter can contain printf formatting styles.
The severity parameter specifies the severity of the exception.
THROW statement
The error_number parameter does not have to be defined in sys.messages.
The message parameter does not accept printf style formatting.
There is no severity parameter. The exception severity is always set to 16.
In C# we can write single if syntax:
string test;
int value=1;
test = value>=1 ? "is bigger or equal one" : "is less than one";
T-SQL in SQL Server 2008 R2 has single IF syntax?
SQL Server 2008 doesn't, you'd have to use a CASE statement...
SQL Server 2012 does have the function:
SELECT IIF ( #FirstArgument > #SecondArgument , 'TRUE', 'FALSE' )
AS [Output Using IIF Logical Function]
DECLARE #test VARCHAR(50);
DECLARE #value INT = 1;
SET #test = CASE WHEN #value >= 1 THEN 'is bigger or equal one' ELSE 'is less than one' END
I am trying to send emails individually to a list of recipients. I am receiving the error:
Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 478
Query execution failed: Msg 4104, Level 16, State 1, Server xxxxxx, Line 1
The multi-part identifier "email#example.com" could not be bound.
Here is a simplified version of my code, asssuming table1 is a valid existing table and name and email are existing columns.
declare #current_mailaddress varchar(50), #query varchar(1000)
set #current_mailaddress = 'email#example.com'
set #query = 'select distinct name, email from table1
where email = ' + #current_email
exec msdb.dbo.sp_send_dbmail
#recipients = #current_email,
#subject = 'test',
#query = #query
So according to the error, the formatting (of presumably the #query) is wrong. I can't figure it out. Any ideas?
You need to put the value of #current_email in quotes:
'SELECT ... WHERE email = ''' + #current_email + ''''
To see why, consider what your query currently looks like without it:
SELECT ... WHERE email = email#example.com
Any time you work with dynamic SQL, it's a good idea to PRINT the variable for debugging if you get a strange error; it's usually the case that the SQL string you've built is not the one you're expecting. I suggested an easy way to manage debugging code in another, unrelated answer.