SQL Server Automated Backups [closed]

If you are using SQL Server Express, you won’t find a UI to run periodic backups. In this case you have to run a batch using Windows Scheduled Tasks or something similar. Don’t forget to use a user with enough privileges to access SQL Server. In the batch file “C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE” -S (local)\SQLExpress … Read more

Storing query results into a variable and modifying it inside a Stored Procedure

Yup, this is possible of course. Here are several examples. — one way to do this DECLARE @Cnt int SELECT @Cnt = COUNT(SomeColumn) FROM TableName GROUP BY SomeColumn — another way to do the same thing DECLARE @StreetName nvarchar(100) SET @StreetName = (SELECT Street_Name from Streets where Street_ID = 123) — Assign values to several … Read more

What does it mean when Statement.executeUpdate() returns -1?

As the statement executed is not actually DML (eg UPDATE, INSERT or EXECUTE), but a piece of T-SQL which contains DML, I suspect it is not treated as an update-query. Section 13.1.2.3 of the JDBC 4.1 specification states something (rather hard to interpret btw): When the method execute returns true, the method getResultSet is called … Read more

Add emoji / emoticon to SQL Server table

Use NVARCHAR(size) datatype and prefix string literal with N: CREATE TABLE #tab(col NVARCHAR(100)); INSERT INTO #tab(col) VALUES (N’👍 🖒 🖓 🖕 🗑 🛦 ⁉ 😎 😔 😇 😥 😴 😭’); SELECT * FROM #tab; db<>fiddle demo Output: ╔═════════════════════════════════╗ ║ col ║ ╠═════════════════════════════════╣ ║ 👍 🖒 🖓 🖕 🗑 🛦 ⁉ 😎 😔 😇 😥 😴😭 … Read more

Finding blocking/locking queries in MS SQL (mssql)

You may find this query useful: SELECT * FROM sys.dm_exec_requests WHERE DB_NAME(database_id) = ‘YourDBName’ AND blocking_session_id <> 0 To get the query itself use this one: SELECT text,* FROM sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle) WHERE DB_NAME(database_id) = ‘YourDBName’ AND blocking_session_id <> 0