tsql

SQL Server: Update table based on JSON

血红的双手。 提交于 2020-01-24 20:30:45
问题 I am trying to update rows in table based on JSON I have. JSON has the following structure: "sensors": [ { "id": "5afd7160f16819f11814f6e2", "num": 0, "name": "AC01", "enabled": true, "unit": "Volt AC Phase 1", "desc": "NAMsdafE", "lt_disaster": 1, "gt_disaster": 1, "lt_high": 1, "gt_high": 1, "lt_average": 1, "gt_average": 1 },... Table dbo.sensors has same structure + few more columns. To insert such JSON object, not array, into table, I would do it this way: INSERT INTO dbo.sensors (......

Unused variable detection in SQL Server

霸气de小男生 提交于 2020-01-24 20:12:34
问题 Is there a way to identify local variables that have been created (or even created and set to a value), but are never used again? I'm thinking along the lines of when an IDE will underline such variables with a squiggly line. If this isn't something that can be enabled within SSMS, are there any add-ins that do this? (Particularly interested in SQL Server 2008 R2, if it matters) Thanks for the insight. 回答1: ApexSQL Refactor, a free SSMS and VS SQL formatting and refactoring add-in, has a

Using TSQL to Unzip a value

放肆的年华 提交于 2020-01-24 19:17:42
问题 How can I unzip a varbinary(max) value in a stored procedure? I'd like to implement or invoke a gunzip algorithm within TSQL code, preferably without enabling the CLR. 回答1: Look into sp_OACreate http://msdn.microsoft.com/en-us/library/ms189763.aspx sp_oamethod http://msdn.microsoft.com/en-us/library/ms174984.aspx You can write the value as a file using file system object, then use a command line gunzip program on it and the read the file using FSO back. Yes this is not practical but I am not

SQL Server: preventing dirty reads in a stored procedure

喜欢而已 提交于 2020-01-24 18:54:09
问题 Consider a SQL Server database and its two stored procs: *1. A proc that performs 3 important things in a transaction: Create a customer, call a sproc to perform another insert, and conditionally insert a third record with the new identity. BEGIN TRAN INSERT INTO Customer(CustName) (@CustomerName) SELECT @NewID = SCOPE_IDENTITY() EXEC CreateNewCustomerAccount @NewID, @CustomerPhoneNumber IF @InvoiceTotal > 100000 INSERT INTO PreferredCust(InvoiceTotal, CustID) VALUES (@InvoiceTotal, @NewID)

How to display roundof time

余生长醉 提交于 2020-01-24 18:12:57
问题 Using SQL Server 2005 Table1 ID Intime Outtime 001 00.21.00 00.48.00 002 08.23.00 13.45.00 003 00.34.00 00.18.00 I need to display the time time like 30 minutes or 1 Hours, it should display a roundoff time Expected Output ID Intime Outtime 001 00.30.00 01.00.00 002 08.30.00 14.00.00 003 01.00.00 00.30.00 How to make a query for the roundoff time. 回答1: You can round the current date to 30 minutes like: select dateadd(mi, datediff(mi,0,getdate())/30*30, 0) Explanation: this takes the number of

Export Image column from SQL Server 2000 using BCP

泪湿孤枕 提交于 2020-01-24 15:11:42
问题 I've been tasked with extracting some data from an SQL Server 2000 database into a flat format on disk. I've little SQL Server experience. There is a table which contains files stored in an "IMAGE" type column, together with an nvarchar column storing the filename. It looks like there are numerous types of files stored in the table: Word docs, XLS, TIF, txt, zip files, etc. I'm trying to extract just one row using BCP, doing something like this: bcp "select file from attachments where id =

Is there a way to pivot a customer ID and a their most recent order dates?

北慕城南 提交于 2020-01-24 13:12:11
问题 I have a query that gives me all customer's and their last three order dates. EX: CustomerId DateOrdered 167 2006-09-16 01:25:38.060 167 2006-09-21 13:11:53.530 171 2006-08-31 15:19:22.543 171 2006-09-01 13:30:54.013 171 2006-09-01 13:34:36.483 178 2006-09-04 11:36:19.983 186 2006-09-05 12:50:27.153 186 2006-09-05 12:51:08.513 I want to know if there is a way for me to pivot it to display like this: [CustomerId] [Most Recent] [Middle] [Oldest] '167' '2006-09-21 13:11:53.530' '2006-09-16 01:25

Is there a way to pivot a customer ID and a their most recent order dates?

非 Y 不嫁゛ 提交于 2020-01-24 13:12:09
问题 I have a query that gives me all customer's and their last three order dates. EX: CustomerId DateOrdered 167 2006-09-16 01:25:38.060 167 2006-09-21 13:11:53.530 171 2006-08-31 15:19:22.543 171 2006-09-01 13:30:54.013 171 2006-09-01 13:34:36.483 178 2006-09-04 11:36:19.983 186 2006-09-05 12:50:27.153 186 2006-09-05 12:51:08.513 I want to know if there is a way for me to pivot it to display like this: [CustomerId] [Most Recent] [Middle] [Oldest] '167' '2006-09-21 13:11:53.530' '2006-09-16 01:25

Find rows where the value contains a word from a list of varchar(50)

本秂侑毒 提交于 2020-01-24 12:36:25
问题 I am collecting data from a t-sql stored procedure to import into c# program. I would like to narrow down the data first. I have data that has three field that describes the three values that follows them. I need to find only the fields that have one of a dozen keywords in the description. I was using something that UNION all the fields with values, then ... AND ( TEXT1234.AccountValue LIKE '%word1%' OR TEXT2345.AccountValue LIKE '%word1%' OR TEXT3456.AccountValue LIKE '%word1%' OR TEXT1234

SQL:Getting count from many tables for a user record in USER table.Whats the best approach?

梦想的初衷 提交于 2020-01-24 12:23:27
问题 I have 4 SQL server(2008 version) tables 1) USER- to store user information (Fields : UserId,UserName) 2) FILES - to store files uploaded by user (FileId,FileName,UserId) 3) PHOTOS -to store files uploaded by user (PhotoId,PhotoName,UserId) 4) GROUPS= to store groups created by user ( GroupId,GroupName,UserId) Now I want to get a USER record with id=5 along with Total number of Files uploaded by userid 5,Total Photos uploaded by user id 5,Total groups created by userid 5.Can i get all these