sql-server-2016

TSQL logging inside transaction using CLR stored procedure

巧了我就是萌 提交于 2020-01-06 02:40:06
问题 I'm trying to write a log into another database inside a transaction so that the log will survive even if the transaction is rolled back. I've read this answer which says: One possibility is to use a CLR stored procedure to do the logging. This can open its own connection to the database outside the transaction and enter and commit the log data. So I created CLR stored procedure using this article: [SqlProcedure] public static void Voice(SqlString procedureName, SqlInt32 id) { Connection =

Query equivalence with DISTINCT

◇◆丶佛笑我妖孽 提交于 2020-01-05 07:32:11
问题 Let us have a simple table order(id: int, category: int, order_date: int) created using the following script IF OBJECT_ID('dbo.orders', 'U') IS NOT NULL DROP TABLE dbo.orders SELECT TOP 1000000 NEWID() id, ABS(CHECKSUM(NEWID())) % 100 category, ABS(CHECKSUM(NEWID())) % 10000 order_date INTO orders FROM sys.sysobjects CROSS JOIN sys.all_columns Now, I have two equivalent queries (at least I believe that they are equivalent): -- Q1 select distinct o1.category, (select count(*) from orders o2

Extended events file store with Azure blob storage

心不动则不痛 提交于 2020-01-05 05:52:35
问题 I have created extended event for azure database using SQL server 2016 for which i am specifying event file from azure blob storage. I have added credential in SQL server with storage access signature of respective blob container. Whenever i start the session i get following error : Any idea what is the issue? 回答1: I have added credential in SQL server with storage access signature of respective blob container. As far as I know, if we do not provide SQL credential or we do not store the

Logging into local SQL Server instance with windows auth

孤街浪徒 提交于 2020-01-05 04:10:11
问题 I have a nodejs and express based project. I currently use sequelize to connect to a SQL Server instance. This works fine for external SQL Servers using SQL Server authentication. However, I am now trying to connect to a local SQL Server instance using Windows auth. This is what my connection string looks like: export var sequelize = new Sequelize({ dialect: 'mssql', dialectModulePath: 'sequelize-msnodesqlv8', dialectOptions: { connectionString: 'Server=localhost\MSSQLSERVER01;Database=DB

Select results from multiple array elements in Json document with Sql Server 2016's OPENJSON function

那年仲夏 提交于 2020-01-05 03:47:11
问题 Is it possible to combine parts of a json document from multiple array elements into a single result in Sql Server 2016? Given this json: { "fruit": { "types": [ { "possible": [ "Apples", "Bananas", "Pears" ], "category": "Basic" }, { "possible": [ "Oranges", "Grapefruit", "Lemons", "Limes" ], "category": "Citrus" }, { "possible": [ "Blueberries", "Strawberries", "Cherries" ], "category": "Berries" } ] } } I'd like to see a single result of all the values from the possible element: results --

Import data tier application is failing

Deadly 提交于 2020-01-04 09:29:08
问题 I'm getting the following error while restoring the database backup ( .bacpac file) into my on premise SQL Server 2016 instance. TITLE: Microsoft SQL Server Management Studio Could not import package. Warning SQL72012: The object [Azure_Data] exists in the target, but it will not be dropped even though you selected the 'Generate drop statements for objects that are in the target database but that are not in the source' check box. Warning SQL72012: The object [Azure_Log] exists in the target,

SQL Server 2016 - Calculating remaining stock quantity

拟墨画扇 提交于 2020-01-04 07:28:08
问题 I have a table like this: As you can see, I have a column called remaining_stock that is correct for the first line of each item but NOT for the subsequent lines. The first line is straight forward as you can simply subtract the order-quantity from initial_stock.. What I want to achieve is to get a remaining_stock column that looks like this: I though of using row_number and then joining back to the same table using the row numbers.. but that doesn't quite work either. can someone point me in

How do I format date in TSql when I use json_value

六眼飞鱼酱① 提交于 2020-01-04 05:19:09
问题 I am using SQL Server 2016 and I am storing json. When I extract it, the datetime format does not show clean. How do I format the datetime format? select JSON_VALUE(trade, '$.entrytime') AS entrytime from dbo.Trades Values shown: 2016-05-23T05:21:30.3068919-04:00 2016-05-24T10:49:16.337257-04:00 2016-05-24T11:05:30.8941267-04:00 2016-05-24T11:37:35.9555731-04:00 How do I format those dates in the yyyy-mm-dd hh:mm:ss format? 回答1: You can also use FORMAT : select FORMAT(CAST(JSON_VALUE(trade, '

Create User in dacpac deployed by SqlPackage.exe leads to login failed SqlException

依然范特西╮ 提交于 2020-01-04 02:06:31
问题 Problem I'm trying to automate SQL deployment including User and Roles. It works when I create the user in Sql Management Studio, but when I deploy dacpack I get SqlException: Login Failed for user 'MyUser' Description I have SQL Server 2016 installed on localhost with both Sql Server and Windows authentication enabled. I have Sql Database project in Visual Studio with these files: MyUser.sql [build action=build] CREATE USER [MyUser] WITH Password='$(MyUserPassword)'; RolesMembership.sql

Duplicates in temporal history table

天大地大妈咪最大 提交于 2020-01-04 02:06:16
问题 We have JCC feed that pumping data from Oracle to SQL Server 2016 . For some unknown reasons there are duplicates in the history table with THE SAME start and end times. How can it happen? I tried to update record with the condition SET Column = Column . In that case I have 2 records where all the fields are the same, but start and end times are different. How can it happen that there are same datetimes? UPDATE : DDL: CREATE TABLE [dbo].[LEASES]( [LEASE_NUMBER] [CHAR](7) NOT NULL, [CREDIT