tsql

How do I compare two columns for equality in SQL Server?

Deadly 提交于 2020-01-19 05:37:25
问题 I have two columns that are joined together on certain criteria, but I would also like to check two see if two other columns are identical and then return a bit field if they are. Is there a simpler solution than using CASE WHEN? Ideally I could just use: SELECT Column1 = Column2 AS MyDesiredResult FROM Table1 INNER JOIN Table2 ON Table1.PrimaryKey = Table2.ForeignKey 回答1: What's wrong with CASE for this? In order to see the result, you'll need at least a byte, and that's what you get with a

Aggregate function in an SQL update query?

隐身守侯 提交于 2020-01-19 02:40:04
问题 I'm trying to set the value in one table to the sum of the values in another table. Something along these lines: UPDATE table1 SET field1 = SUM(table2.field2) FROM table1 INNER JOIN table2 ON table1.field3 = table2.field3 GROUP BY table1.field3 Of course, as this stands, it won't work - SET doesn't support SUM and it doesn't support GROUP BY . I should know this, but my mind's drawing a blank. What am I doing wrong? 回答1: UPDATE t1 SET t1.field1 = t2.field2Sum FROM table1 t1 INNER JOIN (select

Aggregate function in an SQL update query?

荒凉一梦 提交于 2020-01-19 02:39:14
问题 I'm trying to set the value in one table to the sum of the values in another table. Something along these lines: UPDATE table1 SET field1 = SUM(table2.field2) FROM table1 INNER JOIN table2 ON table1.field3 = table2.field3 GROUP BY table1.field3 Of course, as this stands, it won't work - SET doesn't support SUM and it doesn't support GROUP BY . I should know this, but my mind's drawing a blank. What am I doing wrong? 回答1: UPDATE t1 SET t1.field1 = t2.field2Sum FROM table1 t1 INNER JOIN (select

CTE error: “Types don't match between the anchor and the recursive part”

穿精又带淫゛_ 提交于 2020-01-18 15:49:05
问题 I am executing the following statement: ;WITH cte AS ( SELECT 1 as rn, 'name1' as nm UNION ALL SELECT rn + 1, nm = 'name' + CAST((rn + 1) as varchar(255)) FROM cte a WHERE rn < 10) SELECT * FROM cte ...which finishes with the error... Msg 240, Level 16, State 1, Line 2 Types don't match between the anchor and the recursive part in column "nm" of recursive query "cte". Where am I making the mistake? 回答1: Exactly what it says: 'name1' has a different data type to 'name' + CAST((rn+1) as varchar

Query to list all stored procedures

元气小坏坏 提交于 2020-01-18 11:09:26
问题 What query can return the names of all the stored procedures in a SQL Server database If the query could exclude system stored procedures, that would be even more helpful. 回答1: As Mike stated, the best way is to use information_schema . As long as you're not in the master database, system stored procedures won't be returned. SELECT * FROM DatabaseName.INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' If for some reason you had non-system stored procedures in the master database,

Query to list all stored procedures

雨燕双飞 提交于 2020-01-18 11:09:07
问题 What query can return the names of all the stored procedures in a SQL Server database If the query could exclude system stored procedures, that would be even more helpful. 回答1: As Mike stated, the best way is to use information_schema . As long as you're not in the master database, system stored procedures won't be returned. SELECT * FROM DatabaseName.INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' If for some reason you had non-system stored procedures in the master database,

Split a string without delimiter in TSQl

孤街醉人 提交于 2020-01-17 15:28:28
问题 I have a result set from SELECT Statement, how can i split one column without any delimiter this is my result Size TCount TDevice 2 5 E01 4.5 3 W02E01 I want to have this Size TCount TDevice 2 5 E 2 5 0 2 5 1 4.5 3 W 4.5 6 0 (we have 2 times of 0) 4.5 3 2 4.5 3 1 thank you 回答1: ;with cte as ( select Size,TCount, substring(TDevice, 1, 1) as Chars, stuff(TDevice, 1, 1, '') as TDevice from t1 union all select Size,TCount, substring(TDevice, 1, 1) as Chars, stuff(TDevice, 1, 1, '') as TDevice

SQL Query Dynamic PIVOT [duplicate]

≯℡__Kan透↙ 提交于 2020-01-17 12:41:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure: MONTH ID CNT ----- ----------- --- 4 TOTAL_COUNT 214 5 TOTAL_COUNT 23 6 TOTAL_COUNT 23 4 FUNC_COUNT 47 5 FUNC_COUNT 5 6 FUNC_COUNT 5 4 INDIL_COUNT 167 5 INDIL_COUNT 18 6 INDIL_COUNT 18 How i can get the Pivot over month in this table like: ID APRIL MAY JUNE ----------- ----- --- ---- TOTAL_COUNT 214 23 23 FUNC_COUNT 47 5 5 INDIL

Importing a BCP file in Azure database

丶灬走出姿态 提交于 2020-01-17 09:21:52
问题 I have an Azure function that retrieves a zip file that contains multiple BCP files which unzips them and adds them as blobs. I now want to import the BCP files into my SQL database but not sure how to go about it. I know I can use following script and run an SqlCommand: BULK INSERT RegPlusExtract.dbo.extract_class FROM 'D:\local\data\extract_class.bsp' WITH ( FIELDTERMINATOR = '@**@',ROWTERMINATOR = '*@@*') But this obviously does not work as the SQL server doesn't have access to the local

Converting SQL date formats not ending up the same

╄→гoц情女王★ 提交于 2020-01-17 08:11:25
问题 I am trying to remove the timestamp from my date values so I end up with something like mm/dd/yy or mm/dd/yyyy. I referenced many technet, stackoverflow, and w3schools articles but still can't get the date to appear correctly. All of my columns in the table are defined as a datetime and they come from the same table. I am using convert statements like this: CONVERT(VARCHAR(10), E.PD_DT, 101) AS 'Paid Date' In place of 101 I have used 10 and 11, still have the same issue with the data (below).