sql-server-2008-r2

How to add pagination to the following SQL

时间秒杀一切 提交于 2019-12-25 18:25:35
问题 I've got the following SQL and I want to know how to page it: I've got variables for @skip and @top so I can page through.. SELECT ID FROM ( SELECT COUNT(*) AS ID, -1 AS [Weight] FROM Employees i INNER JOIN #WeightedIDs w ON (i.ID = w.ID) WHERE (i.DepartmentID = 10 and i.ShiftID = 2) UNION ALL SELECT i.ID, w.[Weight] FROM Employees i INNER JOIN #WeightedIDs w ON (i.ID = w.ID) WHERE (i.DepartmentID = 10 and i.ShiftID = 2) ) x ORDER BY x.[Weight] ASC UPDATE: I've got the following but it doesn

SQL Server indexing - varchar(100) vs varbinary(100)? [convert data]

ⅰ亾dé卋堺 提交于 2019-12-25 14:19:13
问题 Is it better to set an index (primary or secondary) on a varchar(x) or varbinary(x) column? I wasn't sure it even mattered. However what I see online (Googled - varchar vs varbinary) is that varchar is almost dead or being pushed to the way side. So is this better to index or something? Could it be the type of index? Excellent scenario: Indexing email addresses ( [edit] encrypted byte array {varbinary} or string equivalent {varchar}) Thanks Answer? It seems that indexes on varbinary is the

A user with db_datareader role has SELECT access to all table. How do I exclude one table?

前提是你 提交于 2019-12-25 14:08:58
问题 I have SQL2008R02 database user added to the data reader role. This user can SELECT all tables. But I want to him to restrict him from one table. How do I do this? I do not want to run GRANT SELECT individually on all tables except one. 回答1: You can use a DENY permission on the one table, for example: DENY SELECT ON myTable TO myUser DENY "permissions" are available in SQL 2008 onwards. https://msdn.microsoft.com/en-GB/library/ms188338.aspx 来源: https://stackoverflow.com/questions/26978536/a

A user with db_datareader role has SELECT access to all table. How do I exclude one table?

孤街浪徒 提交于 2019-12-25 14:08:37
问题 I have SQL2008R02 database user added to the data reader role. This user can SELECT all tables. But I want to him to restrict him from one table. How do I do this? I do not want to run GRANT SELECT individually on all tables except one. 回答1: You can use a DENY permission on the one table, for example: DENY SELECT ON myTable TO myUser DENY "permissions" are available in SQL 2008 onwards. https://msdn.microsoft.com/en-GB/library/ms188338.aspx 来源: https://stackoverflow.com/questions/26978536/a

Updating inserted record within MERGE statement in SQL Server 2008 R2

时光毁灭记忆、已成空白 提交于 2019-12-25 08:49:29
问题 I have following code in my SQL Server 2008 R2 stored procedure. In that stored procedure, I am copying one city to another city with it's family and persons. Here I maintain family's source and target id in @FamilyIdMap . left column indicates the codes line no. -- Copy Person 1> DECLARE @PersonIdMap table (TargetId int, SourceId int) 2> MERGE Person as PersonTargetTable 3> USING (SELECT PersonID, FamilyID, PersonName, ParentID FROM Person 4> WHERE FamilyID in (SELECT FamilyID from Family

Calling linked server from trigger

放肆的年华 提交于 2019-12-25 07:14:58
问题 I've created instead of trigger in server A, in which I call a procedure, which in turn calls other procedure in linked server (server B). Trigger(A) -> Procedure (A) -> Procedure (B) But, when trigger executes it throws following error: OLE DB provider "SQLNCLI11" for linked server "xxx" returned message "The partner transaction manager has disabled its support for remote/network transactions.". Msg 7391, Level 16, State 2, Line 1 The operation could not be performed because OLE DB provider

SQL Server 2008 R2 running sum

孤者浪人 提交于 2019-12-25 06:50:07
问题 I have a table like below. row_no and product are PK. +--------+---------+-------+-----+---------------+---------+ | row_no | Product | value | qoh | prev_week_qty | cum_qty | +--------+---------+-------+-----+---------------+---------+ | 1 | pr:1 | 101 | 101 | NULL | NULL | | 2 | pr:1 | 201 | 101 | NULL | 100 | | 3 | pr:1 | 101 | 101 | NULL | NULL | | 4 | pr:1 | 101 | 101 | NULL | NULL | | 5 | pr:1 | 183 | 101 | NULL | -18 | | 6 | pr:1 | 101 | 101 | NULL | NULL | | 7 | pr:1 | 101 | 101 |

SQL server populate a table based on another table with a substring as column name

China☆狼群 提交于 2019-12-25 06:14:11
问题 I would like to populate a table based on a given table: Given table t1: id1 (string), id2 (string), value (float) tyb uanwe_A 6963 tyb uanwe_B 979 tyb uanwe_C 931 I need : id1, id2, vA, vB, vC tyb uanwe 6963 979 931 My SQL server query: select case substring(id2, 6, 1) when 'A' then [value] end as [vA] from t1 but, this does not work for me because I got many "null" in the case that substring(id2, 6, 1) is not 'A'. select case substring(id2, 6, 1) when 'A' then [value] end as [vA] when 'B'

How do I get the time difference between each subsequent row in SQL?

一曲冷凌霜 提交于 2019-12-25 05:05:10
问题 I need the time difference between row 1 and 2. Then for row 2 and row 3. Is there a query that can do this? My co-workers already deemed this impossible. Thanks alot for your help in advance. 2012-03-08 15:08:02.260 2012-03-08 15:08:07.180 2012-03-08 15:15:09.220 2012-03-08 15:15:09.330 2012-03-08 15:15:09.457 2012-03-23 13:06:19.913 2012-03-23 13:06:20.980 2012-03-23 13:06:21.440 2012-03-23 13:06:21.480 2012-03-23 13:06:21.550 2012-03-23 13:06:21.567 回答1: You weren't clear on granularity,

SQL Server Exact Table Transpose

泄露秘密 提交于 2019-12-25 05:03:51
问题 How do you achieve an exact transpose in SQL? Month | High | Low | Avg ------------------------- Jan | 10 | 9 | 9.5 ------------------------- Feb | 8 | 7 | 7.5 ------------------------- Mar | 7 | 6 | 6.5 ------------------------- Result ------ Jan | Feb | Mar -------------------------- High-- 10 | 8 | 7 -------------------------- Low-- 9 | 7 | 6 -------------------------- Avg 9.5 | 7.5 | 6.5 -------------------------- 回答1: In order to get the result, you will first have to unpivot the High ,