tsql

SQL Server - BEFORE INSERT Trigger

我的未来我决定 提交于 2020-07-21 03:50:05
问题 I have two tables as follows: Table 1 'MySkills' [ primary key (EmpId, SkillId)] +----------+------------+---------+----------+-----------+------------+-----------------+----------------+ | EmpId | CategoryId | SkillId | ExpYears | ExpMonths | Experience | RatingSubmitted | RatingApproved | +----------+------------+---------+----------+-----------+------------+-----------------+----------------+ | CSSL9610 | arcgis | arcgis1 | 0.00 | 0.00 | 0.00 | 1.00 | NULL | | CSSL9610 | arcgis | arcgis2 |

Is there a function like isdate() for datetime2?

送分小仙女□ 提交于 2020-07-20 07:46:14
问题 I know there is a function called ISDATE to validate DATETIME columns, but it works only for the SMALLDATETIME and DATETIME types. Is there a similar way to validate the new data type DATETIME2 in SQL Server 2008 and 2012? 回答1: In SQL Server 2012, you can use TRY_CONVERT : SELECT TRY_CONVERT(DATETIME2, '2012-02-02 13:42:55.2323623'), TRY_CONVERT(DATETIME2, '2012-02-31 13:42:55.2323623'); Results: 2012-02-02 13:42:55.2323623 NULL Or TRY_PARSE : SELECT TRY_PARSE('2012-02-02 13:42:55.2323623' AS

Common substring of two string in SQL

做~自己de王妃 提交于 2020-07-20 04:24:07
问题 I need to find common substring (without space) of two strings in SQL. Query: select * from tbl as a, tbl as b where a.str <> b.str Sample data: str1 | str2 | max substring without spaces ----------+-----------+----------------------------- aabcdfbas | rikcdfva | cdf aaab akuc | aaabir a | aaab ab akuc | ab atr | ab 回答1: I disagree with those who say that SQL is not the tool for this job. Until someone can show me a faster way than my solution in ANY programming language, I will aver that SQL

How can I increment the value for each INSERT INTO iteration?

假装没事ソ 提交于 2020-07-19 05:27:14
问题 I have a Query as shown below column1 is int anothercolumn is varchar(100) INSERT INTO TABLE1 (column1,column2) SELECT (MAX(column1) FROM TABLE1)+1 ,anotherColumn FROM TABLE2 Table1 Before Query column1 column2 ------- ------- 3 test1 4 test2 Table1 After Query column1 column2 ------- ------- 3 test1 4 test2 5 anotherVal1 5 anotherVal2 5 anotherVal3 But I want column1 column2 ------- ------- 3 test1 4 test2 5 anotherVal1 6 anotherVal2 7 anotherVal3 How can I achieve this in SQLserver 2008

How can I increment the value for each INSERT INTO iteration?

丶灬走出姿态 提交于 2020-07-19 05:27:10
问题 I have a Query as shown below column1 is int anothercolumn is varchar(100) INSERT INTO TABLE1 (column1,column2) SELECT (MAX(column1) FROM TABLE1)+1 ,anotherColumn FROM TABLE2 Table1 Before Query column1 column2 ------- ------- 3 test1 4 test2 Table1 After Query column1 column2 ------- ------- 3 test1 4 test2 5 anotherVal1 5 anotherVal2 5 anotherVal3 But I want column1 column2 ------- ------- 3 test1 4 test2 5 anotherVal1 6 anotherVal2 7 anotherVal3 How can I achieve this in SQLserver 2008

How to query xml column in TSQL for specific text

风流意气都作罢 提交于 2020-07-16 10:39:37
问题 I am trying to query the word Simple out the following XML Document. The column is xml data type, the column name is InstanceSecurity . <InstanceSecurity> <Folder> <Authorization Mode="Simple"> <Deny PrivilegeCode="Create" /> <Deny PrivilegeCode="Delete" /> <Deny PrivilegeCode="Edit" /> <Deny PrivilegeCode="Read" /> <Deny PrivilegeCode="View" /> <Deny PrivilegeCode="Print" /> <Allow PrivilegeCode="All" EntityType="Personnel" EntityVal="5bc2" /> <Allow PrivilegeCode="All" EntityType="Personnel

How to replicate a SCH_S, SCH_M deadlock

大城市里の小女人 提交于 2020-07-15 20:56:04
问题 I have a large migration script (about 2000 lines) that fails because of a deadlock on some metadata, and the lock types are SCH_S and SCH_M according to some xEvents data I've captured on it. The script is quite complicated, so I'm trying to recreate the same scenario using minimal scripting, so I can investigate it further. One difference between my scenario and some of the ones I've found online is that my migration script is being executed in a single process, as opposed to many examples

Getting the top 6 items in a column to pivot to a row in SQL

独自空忆成欢 提交于 2020-07-15 10:00:17
问题 I'm having trouble getting a column to pivot in SQL. I'd like to pivot the top 6 results from one column into a row. The column I'm pivoting can have less than or more than 6 results to start with but I want to ignore anything beyond the top 6. My Table1 looks like this: ID | GroupID | CodeNum ---------------------- 1 | 1 | 111 2 | 1 | 222 3 | 1 | 333 4 | 1 | 444 5 | 1 | 555 6 | 1 | 666 7 | 1 | 777 8 | 2 | 111 9 | 2 | 888 10 | 3 | 999 And I want my output to look like this: GroupID | Code1 |

Understanding CTE Semicolon Placement

孤街浪徒 提交于 2020-07-10 10:27:20
问题 When I run this CTE in SQL Server it says the syntax is incorrect by the declare statement. ;WITH cte as ( SELECT tblKBFolders.FolderID from tblKBFolders where FolderID = @FolderID UNION ALL SELECT tblKBFolders.FolderID FROM tblKBFolders INNER JOIN cte ON cte.FolderID = tblKBFolders.ParentFolderID ) declare @tblQueryFolders as table (FolderID uniqueidentifier) insert into @tblQueryFolders SELECT FolderID From cte; But if I move the declare to before the CTE, it runs just fine. declare

SQL server - Statistics and Recompiling

二次信任 提交于 2020-07-10 07:34:08
问题 A bulk update was done on a table. This caused an SP to execute very slowly even if it had OPTION(RECOMPILE) . Executing sp_updatestats fixed this. So I assume the statistics stored in 'somewhere' was old and the SP, even if recompiled, used the old statistics. How is statistics and recompiling related? 回答1: Recompiling discards the cached execution plan of your SP and creates a new one. As you've correctly guessed the current statistics will be used for plan generation, so to make sure your