tsql

Calling an in-line TVF with named parameters, what is the proper syntax?

孤街醉人 提交于 2020-02-11 13:30:51
问题 I tried calling an in-line TVF with positional parameters and it works fine: SELECT MyTable.Col1, (SELECT TvfColumn FROM ufnGetData(MyTable.Col1, MyTable.Col2)), MyTable.Col2 FROM MyTable Then I tried to name the parameters to the TVF and it does not parse: SELECT MyTable.Col1, (SELECT TvfColumn FROM ufnGetData(@Param1=MyTable.Col1, @Param2=MyTable.Col2)), MyTable.Col2 FROM MyTable The function looks something like: CREATE FUNCTION dbo.ufnGetData ( @Param1 INT, @Param2 INT ) RETURNS TABLE AS

MERGE - UPDATE column values separately, based on logic in WHEN MATCHED block

我与影子孤独终老i 提交于 2020-02-08 10:05:08
问题 Earlier today, I asked this question and got the answer I was looking for. Now I have a follow-up question: What I want: I want the MERGE to compare each column value, per row, in the target table against the corresponding value in the source table, and make any updates based on the logic separated by OR in the WHEN MATCHED AND block. I am afraid that the code I've written (pictured below) will make the updates listed in the THEN UPDATE SET block if any of the logic separated by OR in the

Rolling up multiple rows in single row

我的梦境 提交于 2020-02-08 03:20:23
问题 I am trying to merge rows of employee DB table Here is my original table I want to merge rows based on department. here is my expected result. I tried using FOR XML PATH('')), 1, 1, '') but I canroll up only one column. I know we have similar question here but its rolling up only one column. any help is much appreciated 回答1: Just use the same method for the other columns: SELECT t.Department, Worker = STUFF(( SELECT ';' + Worker FROM tbl WHERE Department = t.Department ORDER BY Worker FOR XML

Selecting multiple columns from a subquery

依然范特西╮ 提交于 2020-02-07 08:20:11
问题 I've searched a lot, but still no chance on having a subquery to return multiple columns all at once. The following code works, but it sucks: SELECT (SELECT Column1 FROM dbo.fnGetItemPath(ib.Id)) AS Col1, (SELECT Column2 FROM dbo.fnGetItemPath(ib.Id)) AS Col2, (SELECT Column3 FROM dbo.fnGetItemPath(ib.Id)) AS Col3 FROM ItemBase ib I actually have got no idea how to pass ib.Id to the function and get the entire Column1, Column2, Column3 columns without calling the fnGetItemPath function 3

Return list values with function

我怕爱的太早我们不能终老 提交于 2020-02-07 04:57:45
问题 I am trying to write the query and is working fine with the one card_nr. Example: Declare @card_nr as nvarchar(50) set @card_nr = '704487442952000472' select a.times, b.times from (select stat_id, times from info where card_nr = @card_nr) as a left outer join (select stat_id, times from info where card_nr = @card_nr) as b on a.stat_id = b.stat_id+1 And I got the correct result: 2014-02-04 11:20:00.000 NULL 2014-02-06 09:44:00.000 2014-02-04 11:20:00.000 2014-02-12 09:59:00.000 2014-02-06 09

PATINDEX with SOUNDEX

女生的网名这么多〃 提交于 2020-02-07 04:41:07
问题 Want to search the string using PATINDEX and SOUNDEX. I have the following table with some sample data to search the given string using PATINDEX and SOUNDEX . create table tbl_pat_soundex ( col_str varchar(max) ); insert into tbl_pat_soundex values('Smith A Steve'); insert into tbl_pat_soundex values('Steve A Smyth'); insert into tbl_pat_soundex values('A Smeeth Stive'); insert into tbl_pat_soundex values('Steve Smith A'); insert into tbl_pat_soundex values('Smit Steve A'); String to search:-

Corresponding GROUP_CONCAT() in SQL SERVER 2017 [duplicate]

こ雲淡風輕ζ 提交于 2020-02-06 20:16:56
问题 This question already has answers here : How to use GROUP BY to concatenate strings in SQL Server? (18 answers) Closed 3 months ago . I want to convert the following MYSQL query to MS SQL Server. But, I am sure that GROUP_CONCAT doesn't exist in MS SQL Server prior to 2017. There is a function in SQL SERVER 2017. Can anyone help me? SELECT region, GROUP_CONCAT(route_name) AS route_name FROM route_details LEFT JOIN ride_details ON route_id = r_id WHERE region != '' AND service_date = '2019-09

Corresponding GROUP_CONCAT() in SQL SERVER 2017 [duplicate]

牧云@^-^@ 提交于 2020-02-06 20:09:33
问题 This question already has answers here : How to use GROUP BY to concatenate strings in SQL Server? (18 answers) Closed 3 months ago . I want to convert the following MYSQL query to MS SQL Server. But, I am sure that GROUP_CONCAT doesn't exist in MS SQL Server prior to 2017. There is a function in SQL SERVER 2017. Can anyone help me? SELECT region, GROUP_CONCAT(route_name) AS route_name FROM route_details LEFT JOIN ride_details ON route_id = r_id WHERE region != '' AND service_date = '2019-09

Corresponding GROUP_CONCAT() in SQL SERVER 2017 [duplicate]

送分小仙女□ 提交于 2020-02-06 20:09:22
问题 This question already has answers here : How to use GROUP BY to concatenate strings in SQL Server? (18 answers) Closed 3 months ago . I want to convert the following MYSQL query to MS SQL Server. But, I am sure that GROUP_CONCAT doesn't exist in MS SQL Server prior to 2017. There is a function in SQL SERVER 2017. Can anyone help me? SELECT region, GROUP_CONCAT(route_name) AS route_name FROM route_details LEFT JOIN ride_details ON route_id = r_id WHERE region != '' AND service_date = '2019-09

Delete duplicate records only on condition in sql server

你。 提交于 2020-02-06 10:00:31
问题 I want to delete duplicates for example I want to delete this row "Test2, 321, 0" because there is a duplicate, but in row 1 and 2 I only want to delete row two because the type id is higher then the current duplicate in row one. This is my table ID, Name, RecordNum, Type -------------------------- 1, Test, 123, 0 2, Test, 123, 1 3, Test2, 321, 0 4, Test2, 321, 0 I can delete duplicate in row 4 using this query. but I cannot seem to figure out how to delete row 2 because row 1 is the same but