tsql

Populating a Datetime Column

只愿长相守 提交于 2020-01-16 20:42:26
问题 I want to populate a datetime column on the fly within a stored procedure. below is the query that I currently have that does same but slows down query performance. CREATE TABLE #TaxVal ( ID INT , PaidDate DATETIME , CustID INT , CompID INT ) INSERT INTO #TaxVal(ID, PaidDate, CustID, CompID) VALUES(01, '20150201',12, 100) , (03,'20150301', 18,101) , (10,'20150401',19,22) , (17,'20150401',02,11) , (11,'20150411',18,201) , (78,'20150421',18,299) , (133,'20150407',18,101) -- SELECT * FROM

Best way to calculate Max/Min of N columns in SQL Server

北城以北 提交于 2020-01-16 19:38:09
问题 Ok, firstly I've seen this thread. But none of the solutions are very satisfactory. The nominated answer looks like NULLs would break it, and the highest-rated answer looks nasty to maintain. So I was wondering about something like the following : CREATE FUNCTION GetMaxDates ( @dte1 datetime, @dte2 datetime, @dte3 datetime, @dte4 datetime, @dte5 datetime ) RETURNS datetime AS BEGIN RETURN (SELECT Max(TheDate) FROM ( SELECT @dte1 AS TheDate UNION ALL SELECT @dte2 AS TheDate UNION ALL SELECT

Query times out when run from ADO.NET but runs fine in Query Analyer

感情迁移 提交于 2020-01-16 19:28:08
问题 On local development machine my query times out indefinitely when run via website (ASP.NET - ADO.NET) Same query was running just yesterday fine. It runs fine when I execute it from local machine. I even resorted to rebooting machine. What can it be? Explanations per requests: Query times out after default 30 seconds (in ADO.NET). If I set it to 0 (indefinite) - it runs indefinitely. In Query analyzer it runs immediately (1 second) Query didn't change. Here is code for view: . public static

Query times out when run from ADO.NET but runs fine in Query Analyer

跟風遠走 提交于 2020-01-16 19:27:51
问题 On local development machine my query times out indefinitely when run via website (ASP.NET - ADO.NET) Same query was running just yesterday fine. It runs fine when I execute it from local machine. I even resorted to rebooting machine. What can it be? Explanations per requests: Query times out after default 30 seconds (in ADO.NET). If I set it to 0 (indefinite) - it runs indefinitely. In Query analyzer it runs immediately (1 second) Query didn't change. Here is code for view: . public static

T-SQL Max date and min date with value single row

删除回忆录丶 提交于 2020-01-16 18:50:30
问题 First of all I would like to thank the friends who helped this complex and difficult query. I have three tables Table 1 StaffId FirstName LastName staffType --------------------------------------- 1 Adam Sorme Student 2 Lara Sandra Teacher 3 Jack Jones Student Table 2 GateId GateName --------------------------------------- 1 frontDoor 2 superDoor Table 3 Id transitionDate GateId StaffId --------------------------------------- 1 2018-01-1 08:00:00 1 1 2 2018-01-1 10:00:00 2 1 3 2018-01-1 20:00

T-SQL Max date and min date with value single row

孤者浪人 提交于 2020-01-16 18:50:13
问题 First of all I would like to thank the friends who helped this complex and difficult query. I have three tables Table 1 StaffId FirstName LastName staffType --------------------------------------- 1 Adam Sorme Student 2 Lara Sandra Teacher 3 Jack Jones Student Table 2 GateId GateName --------------------------------------- 1 frontDoor 2 superDoor Table 3 Id transitionDate GateId StaffId --------------------------------------- 1 2018-01-1 08:00:00 1 1 2 2018-01-1 10:00:00 2 1 3 2018-01-1 20:00

T-SQL Max date and min date with value single row

有些话、适合烂在心里 提交于 2020-01-16 18:50:06
问题 First of all I would like to thank the friends who helped this complex and difficult query. I have three tables Table 1 StaffId FirstName LastName staffType --------------------------------------- 1 Adam Sorme Student 2 Lara Sandra Teacher 3 Jack Jones Student Table 2 GateId GateName --------------------------------------- 1 frontDoor 2 superDoor Table 3 Id transitionDate GateId StaffId --------------------------------------- 1 2018-01-1 08:00:00 1 1 2 2018-01-1 10:00:00 2 1 3 2018-01-1 20:00

Does SQL Server have something like @@IDENTITY that returns multiple values?

爷,独闯天下 提交于 2020-01-16 13:19:09
问题 I know that when I insert a row into a table with an identity column, I can retrieve the ID of the row I just inserted using SELECT @@IDENTITY . If I do an insert statement that inserts ten rows into that same table, is there a practical way to get the IDs of all ten rows? 回答1: Yes, use the OUTPUT clause. You should be using that as a first choice anyway. Even better you can return more than one field with OUTPUT. SO you can popuate a table variable with the surrogate key and the natural key.

Computed column expression

白昼怎懂夜的黑 提交于 2020-01-16 13:17:30
问题 I have a specific need for a computed column called ProductCode ProductId | SellerId | ProductCode 1 1 000001 2 1 000002 3 2 000001 4 1 000003 ProductId is identity, increments by 1. SellerId is a foreign key. So my computed column ProductCode must look how many products does Seller have and be in format 000000. The problem here is how to know which Sellers products to look for? I've written have a TSQL which doesn't look how many products does a seller have ALTER TABLE dbo.Product ADD

How to invert words in a column

依然范特西╮ 提交于 2020-01-16 11:39:30
问题 I have a table with varchar(128) column with data like: word1, word2, , word3, word4 word1, word2, word3, , word1,,,, ; word2 ;word1 word2, word3 I have to update this table, making words reverse order: word4, ,word3,,word2 ,word1 ,,word3, ,word2, word1 Could you help me to do it using only single sql query?? 回答1: To accomplish this task will require a t-sql function and a cursor. fn_SplitList will allow you to split based on a delimiter. Once you have this function you can create a cursor to