tsql

Using Optional parameters when incoming value is null?

左心房为你撑大大i 提交于 2020-07-09 12:16:06
问题 I have a stored procedure that looks like the following. Let's say I call the procedure usp_Exclusion 'Joe', null, 'Bob' , what would I need to do in the stored procedure so that the null value changes to a string (in this case, '')? Unless it's the only way, I don't want to go through every parameter and set to a string if it's null. ALTER procedure dbo.usp_Exclusion ( @exclude1 varchar(100) = '', @exclude2 varchar(100) = '', @exclude3 varchar(100) = '' ) as declare @table table ( EmpName

SQL Server DATEDIFF Computed Column Between Rows

折月煮酒 提交于 2020-07-09 04:43:49
问题 I currently have a table similar to this - RecordTime Running Fault ----------------------------------------------- 2016-09-15 11:32:01.00 0 202 2016-09-15 11:32:08.00 1 202 2016-09-15 11:39:31.00 0 21 2016-09-15 11:40:07.00 1 4 2016-09-15 11:42:11.00 0 21 2016-09-15 11:42:39.00 1 45 I then wanted to calculate the time difference between the RecordTimes for each record. For this I am using the following - WITH rows AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY RecordTime) AS rn FROM dbo.Table1 )

TSQL - make a literal float value

空扰寡人 提交于 2020-07-08 08:07:50
问题 I understand the host of issues in comparing floats, and lament their use in this case - but I'm not the table author and have only a small hurdle to climb... Someone has decided to use floats as you'd expect GUIDs to be used. I need to retrieve all the records with a specific float value. sp_help MyTable -- Column_name Type Computed Length Prec -- RandomGrouping float no 8 53 Here's my naive attempt: --yields no results SELECT RandomGrouping FROM MyTable WHERE RandomGrouping = 0

COALESCE - guaranteed to short-circuit?

こ雲淡風輕ζ 提交于 2020-07-04 12:03:38
问题 From this question, a neat answer about using COALESCE to simplify complex logic trees. I considered the problem of short circuiting. For instance, in functions in most languages, arguments are fully evaluated and are then passed into the function. In C: int f(float x, float y) { return x; } f(a, a / b) ; // This will result in an error if b == 0 That does not appear to be a limitation of the COALESCE "function" in SQL Server: CREATE TABLE Fractions ( Numerator float ,Denominator float )

COALESCE - guaranteed to short-circuit?

蹲街弑〆低调 提交于 2020-07-04 12:02:23
问题 From this question, a neat answer about using COALESCE to simplify complex logic trees. I considered the problem of short circuiting. For instance, in functions in most languages, arguments are fully evaluated and are then passed into the function. In C: int f(float x, float y) { return x; } f(a, a / b) ; // This will result in an error if b == 0 That does not appear to be a limitation of the COALESCE "function" in SQL Server: CREATE TABLE Fractions ( Numerator float ,Denominator float )

Changing the maximum length of a varchar column?

天大地大妈咪最大 提交于 2020-07-04 05:46:09
问题 I'm trying to update the length of a varchar column from 255 characters to 500 without losing the contents. I've dropped and re-created tables before but I've never been exposed to the alter statement which is what I believe I need to use to do this. I found the documentation here: ALTER TABLE (Transfact-SQL) however I can't make heads or tails of it. I have the following so far (essentially nothing unfortunately): alter table [progennet_dev].PROGEN.LE alter column UR_VALUE_3 How do I

XML Column - three levels hierarchy - with Cross Apply

独自空忆成欢 提交于 2020-06-29 06:42:22
问题 My prior question was solved here. Now I'm adding one more level of complexity to it - data that is nested parent, child, grandchild. You can see and run sample here: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=df2766c95383d4c8c2d1f55539634341 Sample Code, where Leg1 might be the trip out, and Leg2 might be the trip back. Each leg can have one or more flights. DECLARE @xml XML=' <Reservation> <Name>Neal</Name> <Leg seq=''1''> <Flight>12</Flight> </Leg> <Leg seq=''2''> <Flight>34</Flight>

Unexpected data at typical recursion

醉酒当歌 提交于 2020-06-29 03:53:08
问题 It's hard for me to use words to describe this, so here's the sample: select * into t from (values (10, 'A'), (25, 'B'), (30, 'C'), (45, 'D'), (52, 'E'), (61, 'F'), (61, 'G'), (61, 'H'), (79, 'I'), (82, 'J') ) v(userid, name) Notice how F,G and H have the same userid. Now, consider the following recursive query: with tn as ( select t.userId,t.name, row_number() over (order by userid,newid()) as seqnum from t ), cte as ( select userId, name, seqnum as seqnum from tn where seqnum = 1 union all

How to clear Query Messages?

筅森魡賤 提交于 2020-06-28 14:08:42
问题 How can I clear the Messages buffer in a query? actually I don't want to see any messages there after my query finishes. consider that, I'm using PRINT statement in my query! also my query may print some error messages with/without RAISEERROR . 回答1: No there is actually no way to hide custom PRINT messages. You can only hide Number of rows effected (SET NOCOUNT ON;) SQL Warning (SET ANSI_WARNINGS OFF;) Consider below example SET NOCOUNT ON; SET ANSI_WARNINGS OFF; BEGIN TRY SELECT 1 PRINT

How to clear Query Messages?

痴心易碎 提交于 2020-06-28 14:07:22
问题 How can I clear the Messages buffer in a query? actually I don't want to see any messages there after my query finishes. consider that, I'm using PRINT statement in my query! also my query may print some error messages with/without RAISEERROR . 回答1: No there is actually no way to hide custom PRINT messages. You can only hide Number of rows effected (SET NOCOUNT ON;) SQL Warning (SET ANSI_WARNINGS OFF;) Consider below example SET NOCOUNT ON; SET ANSI_WARNINGS OFF; BEGIN TRY SELECT 1 PRINT