sql-server-2012

Correct way to execute 2 SQL commands without other command executing in between

依然范特西╮ 提交于 2020-01-04 15:29:04
问题 The suggested duplicate doesn't answer the question in the title. I want to execute two MSSQL commands without any other "user" (I'm not sure what's the correct term) executing a command between them. Searching, I have found two ways that seem like they would achieve that but am unsure about them: Use TABLOCK. - But I've seen it being considered bad practice. Use a transaction - But all I could find was that it will be atomic, and not necessarily locking out other actions. What way is the

SQL Server Like Query not case sensitive

二次信任 提交于 2020-01-04 15:15:31
问题 Query SELECT * from Table_2 WHERE name like ('Joe'); Output 1 100 Joe 2 200 JOE 3 300 jOE 4 400 joe Why is it not case sensitive? 回答1: Problem: Query not case sensitive Cause: Column 'Name' has a case-insensitive ( CI ) collation. Solution: You have to use a CS collation: SELECT * FROM fn_helpcollations() WHERE description LIKE N'%case-sensitive%' . Note: There is a database collation and column level collation. And, there is, also, a server level collation. SELECT DATABASEPROPERTYEX(DB_NAME(

Converting datatype Char to Nvarchar

僤鯓⒐⒋嵵緔 提交于 2020-01-04 13:47:57
问题 I am currently trying to convert all columns in all tables of my database from char to nvarchar in SQL server 2012. the query I am trying to run is select cast((select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where DATA_TYPE = 'char') as nvarchar(MAX)) Not sure if I should be changing the data type through INFORMATION_SCHEMA.COLUMNS but I found this to be the best way to grab the datatype. the error I am currently getting is: Subquery returned more than 1 value. This is not permitted when

T-SQL Using Cross-Apply with Delete Statement

前提是你 提交于 2020-01-04 07:02:39
问题 I have the following tables: RecordID 101 102 103 104 105 106 TableOne 101 102 103 104 TableTwo TableThree 101 102 and I need to delete the RecordsID rows, that are not included in the other tables. Please, note that sometimes the one of the tables TableOne,TableTwo,TableThree could be empty and no records should be deleted then. The result table should be: RecordID 101 102 Because of the empty tables I am not able to use INNER JOIN. And because I am using these code in a function I am not

SQL keeping count of occurrences through a sliding window

[亡魂溺海] 提交于 2020-01-04 06:44:14
问题 In the previous question (Please refer to: SQL Keeping count of occurrences) I needed to count the number of occurrences of a variable. The code provided was as follows: SELECT [Date], Code, [Count] = COUNT(*) OVER (PARTITION BY Code ORDER BY [Date] ROWS UNBOUNDED PRECEDING) FROM dbo.YourTable ORDER BY [Date]; However, now I need to introduce an improvement to that code: Let's say that I have the following table: Date | Code ------------------------ 2010/01/01 | 25 2010/01/01 | 22 2010/01/01

how to make raiserror with nowait work with sqlcmd?

廉价感情. 提交于 2020-01-04 06:38:26
问题 Here is the command line: sqlcmd -S localhost -U myuser -P mypwd -b -r0 -Q "raiserror('hello',10,1) with nowait;waitfor delay '00:00:10';raiserror('world!',10,1) with nowait" It is supposed to output hello immediately and then after 10 seconds world! . Unfortunately, it outputs both strings after 10 seconds. Can sqlcmd do it right? How? P.S. I do not want to use LinqPad or any GUI for that matter. 回答1: I realize this is an old post and there is a high probability that no one will see my

Insert statement into SQL Server database

一个人想着一个人 提交于 2020-01-04 06:10:21
问题 I'm trying to find this bug for the last several days, but without any success. I'm trying to insert one new row in a database. Everything goes well: there is no error, and no program crashes. My INSERT statement looks like this: INSERT INTO Polozaj(Znesek, Uporabnik, Cas, Kupec, Popust, Poln_znesek) VALUES(1,1,1,1,1,1) That statement is ok, because when I run the query in my database it adds the new row. My C# code looks like this: string connection = @"Data Source=(LocalDB)\v11.0

SQL Server 2012: Login to PC-NAME\SQLEXPRESS works, but not to local/(local)

感情迁移 提交于 2020-01-04 05:28:46
问题 I'd like to login to my SQL Server 2012 Express with servername local or (local) but it doesn't work. The only servername which works is PC-Name\SQLEXPRESS . local/(local) has to work somehow because I have to use the following connection string: Data Source=localhost;Initial Catalog=... 回答1: I've reinstalled it and ensures to select "default instance" instead of "named instance" during the installation. Now everything is fine again. 回答2: I think I see what you're trying to do now. If you

Invalid length parameter passed to the RIGHT function

懵懂的女人 提交于 2020-01-04 05:18:05
问题 About 2 weeks ago some SQL that has been working for the past 6 months started throwing an error without anything changing. I spent a couple days trying to find out what was going on and it just did not make any sense. Eventually, I tried just an "alter" on the Stored Procedure (which had no effect) and then eventually dropped and recreated it (which did fix it). Then, 2 days ago, the error came back and this time even the drop/create is not fixing it. According to my IT guys, they have not

How can I delete a user from sql server 2012 who owns a schema

大憨熊 提交于 2020-01-04 04:53:34
问题 I created a new user in Sql Server 2012 and "by accident" I marked them as owner of multiple schemas. I meant to mark them as members of the schema but I was on the wrong tab! Because they are now owners of the schema I can't unselect the ownership and I can't delete the user either. How can I undo my mistake? 回答1: You must transfer ownership of the schema to some other user, probably dbo , prior to removing the user: To test this, I did the following: Create a user to own the schema, and a