sql-server-2016

Always encrypted Behavior in SQL Server 2016

心不动则不痛 提交于 2019-11-26 14:42:57
问题 I was doing some demo in SQL Server 2016 for topic Always encrypted. Got few doubts. Below are the steps followed: In Database server (hosted in Microsoft Azure VM): In table MyTable , Created the Column Encryption Key (CEK) and Master Encryption Key (CMK) Select * from MyTable , shows encrypted data.(both from App and DB server) Exported the certificate from Database Server Imported the certificate in App Server (my Local machine) Added Column Encryption Setting=Enabled to the connection

Create nested JSON arrays using FOR JSON PATH

萝らか妹 提交于 2019-11-26 12:19:57
问题 I need to create a JSON output from a query that uses inner join between two tables with a one to many relationship. I would like the values of the secondary table to be nested as array properties of the primary table. Consider the following example: DECLARE @Persons AS TABLE ( person_id int primary key, person_name varchar(20) ) DECLARE @Pets AS TABLE ( pet_owner int, -- in real tables, this would be a foreign key pet_id int primary key, pet_name varchar(10) ) INSERT INTO @Persons (person_id

How do I move a table into a schema in T-SQL

守給你的承諾、 提交于 2019-11-26 11:53:34
问题 I want to move a table into a specific Schema using T-SQL? I am using SQL Server 2008. 回答1: ALTER SCHEMA TargetSchema TRANSFER SourceSchema.TableName; If you want to move all tables into a new schema, you can use the undocumented (and to be deprecated at some point, but unlikely!) sp_MSforeachtable stored procedure: exec sp_MSforeachtable "ALTER SCHEMA TargetSchema TRANSFER ?" Ref.: ALTER SCHEMA SQL 2008: How do I change db schema to dbo 回答2: Short answer: ALTER SCHEMA new_schema TRANSFER old

SQL to JSON - array of objects to array of values in SQL 2016

≡放荡痞女 提交于 2019-11-26 05:32:45
问题 SQL 2016 has a new feature which converts data on SQL server to JSON. I am having difficulty in combining array of objects into array of values i.e., EXAMPLE - CREATE TABLE #temp (item_id VARCHAR(256)) INSERT INTO #temp VALUES (\'1234\'),(\'5678\'),(\'7890\') SELECT * FROM #temp --convert to JSON SELECT (SELECT item_id FROM #temp FOR JSON PATH,root(\'ids\')) RESULT - { \"ids\": [{ \"item_id\": \"1234\" }, { \"item_id\": \"5678\" }, { \"item_id\": \"7890\" }] } But I want the result as - \"ids

Is there a LastIndexOf in SQL Server?

强颜欢笑 提交于 2019-11-26 05:32:12
问题 I am trying to parse out a value from a string that involves getting the last index of a string. Currently, I am doing a horrible hack that involves reversing a string: SELECT REVERSE(SUBSTRING(REVERSE(DB_NAME()), 1, CHARINDEX(\'_\', REVERSE(DB_NAME()), 1) - 1)) To me this code is nearly unreadable. I just upgraded to SQL Server 2016 and I hoping there is a better way. Is there? 回答1: If you want everything after the last _ , then use: select right(db_name(), charindex('_', reverse(db_name())

How to check if a column exists in a SQL Server table?

与世无争的帅哥 提交于 2019-11-26 01:24:15
问题 I need to add a specific column if it does not exist. I have something like the following, but it always returns false: IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \'myTableName\' AND COLUMN_NAME = \'myColumnName\') How can I check if a column exists in a table of the SQL Server database? 回答1: SQL Server 2005 onwards: IF EXISTS(SELECT 1 FROM sys.columns WHERE Name = N'columnName' AND Object_ID = Object_ID(N'schemaName.tableName')) BEGIN -- Column Exists END Martin