sql-server-2014

SQL Pivot with dynamic generated columns, aggregate function and columns without aggregation

微笑、不失礼 提交于 2019-12-05 08:18:39
I have got the following query: WITH preEKBE AS( SELECT EKPO . MANDT, EKPO . EBELN, EKPO . EBELP, DD07T.DDTEXT AS c_Meaning, EKBE . VGABE, EKBE . DMBTR, EKBE . MENGE, COUNT(VGABE) OVER(PARTITION BY EKBE . EBELN, EKBE . EBELP, ZEKKN) AS c_COUNT, CONVERT (varchar(10),MIN(EKBE . BLDAT) OVER ( PARTITION BY EKBE . EBELN, EKBE . EBELP, EKBE . VGABE),104) AS c_EBKE_BLDAT_First, CONVERT (varchar(10),MIN(EKBE . BUDAT) OVER ( PARTITION BY EKBE . EBELN, EKBE . EBELP, EKBE . VGABE),104) AS c_EKBE_BUDAT_First, CONVERT (varchar(10),MAX(EKBE . BLDAT) OVER ( PARTITION BY EKBE . EBELN, EKBE . EBELP, EKBE .

How to make JSON from SQL query in MS SQL 2014

允我心安 提交于 2019-12-04 19:43:10
问题 Question: What is best solution to generate JSON from a SQL query in MS SQL 2014? I created a procedure, but it is very slow. My Example: DECLARE @customers xml; DECLARE @json NVARCHAR(max); SET @customers = (SELECT * FROM dbo.Customers FOR XML path, root) EXEC [dbo].[HTTP_JSON] @customers, @json EXEC [dbo].[HTTP_JSON](@Shopping) Create PROCEDURE [dbo].[HTTP_JSON] @parameters xml, @response NVARCHAR(max) OUTPUT WITH EXEC AS CALLER AS set @response = (SELECT Stuff( (SELECT * from (SELECT ', {'

C# & SQL Server - best way to delete multiple Rows in “one go” using a stored procedure

孤街浪徒 提交于 2019-12-04 15:51:35
I know there's many of the same subjected question here in SO, my question is if I want to delete say around 1K rows rather few, given a List<int> of RecordID , I could avoid using a DataTable , and send the list translated into a statement: string ParmRecordsToDelete_CsvWhereIN = "(" for(int CurIdx=0; CurIdx < RecIdsToDelete.Count; CurIdx++) { ParmRecordsToDelete_CsvWhereIN += RecIdsToDelete[CurIdx] + ", "; //this method to create passed parameter //logic to remove on last Coma on last Index.. //or use stringJoin and somehow remove the last coma } ParRecordsToDelete_CsvWhereIN +=")"; This

SQL Server 2014 execution plan creation takes a long time (fast in older versions)

感情迁移 提交于 2019-12-04 10:36:43
I've encountered a problem with a query in SQL Server 2014. The first time this query is run, it takes ages for the execution plan to be generated. The strange thing is that it has worked just fine in all previous versions of SQL Server (2012, 2008 R2, 2008, etc.). It seems to be related to a unique index on one of the involved tables, combined with a certain amount of subqueries in the main query. Here are the involved tables in the query. I've simplified the tables a lot compared to the originals, but the problem persists. Note the unique constraint on table2, this seems to be what is

How to create function for count number of tags in SQL Server

不打扰是莪最后的温柔 提交于 2019-12-04 06:02:13
问题 In SQL Server 2014, I need a function that can count the number of tags, and if not equal the number of tags open and close tags, tag if the tag is low, add the appropriate package. For this example or source code of page of website: <div> <ul> <li>John</li> <li>sara</li> <li>mack</li> <li>jane<li> </div> count ( < ) = count ( > ) count ( <tag> ) = count ( </tag> ) if count( < ) < count( > ) --> add < before element of tag if count( <tag> ) > count( </tag> ) then add </tag> in correct

Cannot insert the value NULL into column where using Sql Select statement

强颜欢笑 提交于 2019-12-04 05:37:41
问题 Can you tell me how to avoid below mentioned exception ? INSERT INTO [Migrated].[dbo].[Cities] (Name, ZipCode) SELECT DISTINCT a.City, a.ZipCode FROM [Legacy].[dbo].[Ziplist] AS a WHERE (a.City IS NOT NULL AND a.ZipCode IS NOT NULL); Exception: Cannot insert the value NULL into column 'IsDeleted', table 'Migrated.dbo.Cities'; column does not allow nulls. INSERT fails. 回答1: As @jamieD77 commented you are missing the IsDeleted column in your insert statement. The error means that the column is

How to dump an entire SQL Server 2014 database into a file, to be imported into a Postgres database?

假如想象 提交于 2019-12-04 05:14:56
问题 I have a SQL Server 2014 database from which I need to dump just the table data (no indexes, stored procedures, or anything else). This dump needs to be imported into a Postgres 9.3 database "as-is". What id the proper command line to create such a dump? 回答1: I must admit, this is more sort of a joke... You should rather follow the hint to use "Export" and write this to some kind of CSV. Just for fun: EDIT: create a column list to avoid binary columns... columns, which are not directly

VS 2015 Native compiler return value -1073741511 on .rdlc files when building project

懵懂的女人 提交于 2019-12-04 03:21:35
问题 I got this error: rsUnexpectedCompilerError: An unexpected error occurred while compiling expressions. Native compiler return value: ‘-1073741511’ ,when compiling rdlc 's on a new box on Visual Studio 2015 on a Windows 8.1 box for an existing project This happens on every existing r d lc file in the project that has an expression . I checked that. I have the latest sql server data tools (ssdt) installed. I also created a brand new console application with empty report with no expressions and

The value is returned instead of NULL when using function with OUTER APPLY

时光毁灭记忆、已成空白 提交于 2019-12-04 00:34:08
I am getting strange results when using inline function. Here is the code: IF EXISTS ( SELECT * FROM sys.objects AS o WHERE name = 'vendor_relation_users' ) DROP FUNCTION dbo.vendor_relation_users; GO CREATE FUNCTION [dbo].[vendor_relation_users] ( @user_name CHAR(12) ) RETURNS TABLE AS RETURN (SELECT @user_name AS user_name WHERE @user_name NOT LIKE '06%'); GO DECLARE @u CHAR(12) = '066BDLER' SELECT a.user_name, is_v.user_name FROM (SELECT @u AS user_name) a OUTER APPLY [dbo].[vendor_relation_users](@u) AS is_v SELECT a.user_name, is_v.user_name FROM (SELECT @u AS user_name) a OUTER APPLY

SQL Server bug or feature? Decimal numbers conversion

夙愿已清 提交于 2019-12-04 00:06:19
During development faced up with quite a strange SQL Server behavior. Here we have absolutely the same formula for absolutely the same number. The only difference is how we are getting this number (4.250). From table, temp table, variable table or hardcoded value. Rounding and casting is absolutely the same in all cases. -- normal table CREATE TABLE [dbo].[value] ( [val] [decimal] (5, 3) NOT NULL ) INSERT INTO [value] VALUES (4.250 ) SELECT ROUND(CAST(val * 0.01 / 12 AS DECIMAL(15, 9)), 9) AS val FROM [value] AS pr -- inline query from normal table SELECT * FROM (SELECT ROUND(CAST(val * 0.01 /