sql-server-2012

SQL to get next not null value in column

时光总嘲笑我的痴心妄想 提交于 2020-03-04 06:43:17
问题 How can I get next not null value in column? I have MSSQL 2012 and table with only one column. Like this: rownum Orig ------ ---- 1 NULL 2 NULL 3 9 4 NULL 5 7 6 4 7 NULL 8 9 and I need this data: Rownum Orig New ------ ---- ---- 1 NULL 9 2 NULL 9 3 9 9 4 NULL 7 5 7 7 6 4 4 7 NULL 5 8 9 5 Code to start: declare @t table (rownum int, orig int); insert into @t values (1,NULL),(2,NULL),(3,9),(4,NULL),(5,7),(6,4),(7,NULL),(8,9); select rownum, orig from @t; 回答1: One method is to use outer apply :

SQL Server Management Studio crashes after opening database diagram

僤鯓⒐⒋嵵緔 提交于 2020-03-01 02:06:47
问题 I have 2 existing database diagram in local server in SQL Server 2018, SSMS 18.1 While opening it, the application becomes "Not Responding" then crashes without any error. It was working fine for the last 7 days. 回答1: By mean 18, if you have SSMS version 18.0 installed then Database Diagrams were deprecated and removed from 18.0 version, and added back into SSMS version 18.1. Source: Deprecated and removed features in 18.0: check out Deprecated and removed features in 18.0 in above Release

Is it possible to modify system stored procedure?

瘦欲@ 提交于 2020-02-27 08:22:37
问题 I would like to alter sp_helpindex system procedure, or rather replace it with my version. The reasons are: show index_id show included columns show filter definition show fill factor show index sizes show heap info as well Also, interestingly enough my version turned out to be better performing (faster, fewer reads, no cursor). I tried the following, but none of these worked: sp_rename alter procedure drop procedure The error Invalid object name 'sys.sp_helpindex'. The only option I can

Strange TSQL behavior with COALESCE when using Order By [duplicate]

怎甘沉沦 提交于 2020-02-16 05:21:06
问题 This question already has answers here : nvarchar concatenation / index / nvarchar(max) inexplicable behavior (2 answers) Closed 4 years ago . I'm having some very strange behavior with coalesce. When I don't specify a return amount (TOP (50)) I'm only getting a single last result, but if I remove the "Order By" it works... Examples below DECLARE @result varchar(MAX) SELECT @result = COALESCE(@result + ',', '') + [Title] FROM Episodes WHERE [SeriesID] = '1480684' AND [Season] = '1' Order by

Strange TSQL behavior with COALESCE when using Order By [duplicate]

[亡魂溺海] 提交于 2020-02-16 05:15:43
问题 This question already has answers here : nvarchar concatenation / index / nvarchar(max) inexplicable behavior (2 answers) Closed 4 years ago . I'm having some very strange behavior with coalesce. When I don't specify a return amount (TOP (50)) I'm only getting a single last result, but if I remove the "Order By" it works... Examples below DECLARE @result varchar(MAX) SELECT @result = COALESCE(@result + ',', '') + [Title] FROM Episodes WHERE [SeriesID] = '1480684' AND [Season] = '1' Order by

Comparing Parent-child combination from 2 different tables in SQL Server

痴心易碎 提交于 2020-02-06 07:59:01
问题 I have table MAT_ITEM and data as follows +--------+-------+ | MAT_NO | CHILD | +--------+-------+ | 9856 | 874 | | 9856 | 856 | | 9856 | 548 | | 9883 | 596 | | 9883 | 356 | | 7845 | 101 | | 7845 | 908 | | 7845 | 206 | +--------+-------+ Another table MAT_REL and data as follows: +--------+----------+----------+ | MAT_NO | PARENT | CHILD | +--------+----------+----------+ | 9856 | | STEEL | | 9856 | STEEL | 874 | | 9856 | STEEL | 856 | | 9856 | STEEL | 548 | | 9856 | A-STEEL | 874 | | 9856 |

Comparing Parent-child combination from 2 different tables in SQL Server

那年仲夏 提交于 2020-02-06 07:57:12
问题 I have table MAT_ITEM and data as follows +--------+-------+ | MAT_NO | CHILD | +--------+-------+ | 9856 | 874 | | 9856 | 856 | | 9856 | 548 | | 9883 | 596 | | 9883 | 356 | | 7845 | 101 | | 7845 | 908 | | 7845 | 206 | +--------+-------+ Another table MAT_REL and data as follows: +--------+----------+----------+ | MAT_NO | PARENT | CHILD | +--------+----------+----------+ | 9856 | | STEEL | | 9856 | STEEL | 874 | | 9856 | STEEL | 856 | | 9856 | STEEL | 548 | | 9856 | A-STEEL | 874 | | 9856 |

How to Restore data from MS SQL 2012 to MS SQL 2008 using .bak file. Is this Possible?

北战南征 提交于 2020-02-06 06:52:07
问题 I have a requirement that am facing difficulty i.e. restoring a data from MS SQL 2012 to MS SQL 2008 using .bak file. when am trying to restore , am getting an error. is this possibility to restore this to downgrade. can any one help me how to restore ? 回答1: Unfortunately SQL Server will not go backwards. The only way you can do this is Using SSIS to transfer the data or you can use the import/export utility. On your 2012 Server Right click on the DB you want to export data from and scroll

Incrementing custom primary key values in SQL

我与影子孤独终老i 提交于 2020-02-05 05:42:09
问题 I am asked to generate custom ID values for primary key columns. The query is as follows, SELECT * FROM SC_TD_GoodsInward WHERE EntityId = @EntityId SELECT @GoodsInwardId=IIF((SELECT COUNT(*) FROM SC_TD_GoodsInward)>0, (Select 'GI_'+RIGHT('00'+CONVERT(varchar,datepart(YY,getdate())),2)+RIGHT('00'+CONVERT(varchar,datepart(MM,getdate())),2)+RIGHT('00'+CONVERT(varchar,datepart(DD,getdate())),2)+'_'+CONVERT(varchar,@EntityId)+'_'+(SELECT RIGHT('0000'+CONVERT(VARCHAR,CONVERT(INT,RIGHT(MAX

Incrementing custom primary key values in SQL

蓝咒 提交于 2020-02-05 05:42:07
问题 I am asked to generate custom ID values for primary key columns. The query is as follows, SELECT * FROM SC_TD_GoodsInward WHERE EntityId = @EntityId SELECT @GoodsInwardId=IIF((SELECT COUNT(*) FROM SC_TD_GoodsInward)>0, (Select 'GI_'+RIGHT('00'+CONVERT(varchar,datepart(YY,getdate())),2)+RIGHT('00'+CONVERT(varchar,datepart(MM,getdate())),2)+RIGHT('00'+CONVERT(varchar,datepart(DD,getdate())),2)+'_'+CONVERT(varchar,@EntityId)+'_'+(SELECT RIGHT('0000'+CONVERT(VARCHAR,CONVERT(INT,RIGHT(MAX