Pass table as parameter into sql server UDF

You can, however no any table. From documentation: For Transact-SQL functions, all data types, including CLR user-defined types and user-defined table types, are allowed except the timestamp data type. You can use user-defined table types. Example of user-defined table type: CREATE TYPE TableType AS TABLE (LocationName VARCHAR(50)) GO DECLARE @myTable TableType INSERT INTO @myTable(LocationName) VALUES(‘aaa’) … Read more

Rename SQL Server Schema

If you have a large number of objects in a schema, you can use something like this to generate all the changes automatically (it only does tables and views, so before you run it, you might need to expand it to SPs, UDFs, etc.) USE SandBox DECLARE @OldSchema AS varchar(255) DECLARE @NewSchema AS varchar(255) DECLARE … Read more

Drop primary key using script in SQL Server database [duplicate]

You can look up the constraint name in the sys.key_constraints table: SELECT name FROM sys.key_constraints WHERE [type] = ‘PK’ AND [parent_object_id] = Object_id(‘dbo.Student’); If you don’t care about the name, but simply want to drop it, you can use a combination of this and dynamic sql: DECLARE @table NVARCHAR(512), @sql NVARCHAR(MAX); SELECT @table = N’dbo.Student’; … Read more

Update if different/changed

During query compilation and execution, SQL Server does not take the time to figure out whether an UPDATE statement will actually change any values or not. It just performs the writes as expected, even if unnecessary. In the scenario like update table1 set col1 = ‘hello’ you might think SQL won’t do anything, but it … Read more

Drop all databases from server

You can do this through the SSMS GUI. Select the Databases node then F7 to bring up Object Explorer Details, Select all databases that you want to delete, Hit “Delete” and select the “Close Existing Connections” and “Continue after error” options. Alternatively through TSQL you can do EXEC sp_MSforeachdb ‘ IF DB_ID(”?”) > 4 BEGIN … Read more

What’s the fastest way to bulk insert a lot of data in SQL Server (C# client)

Here’s how you can disable/enable indexes in SQL Server: –Disable Index ALTER INDEX [IX_Users_UserID] SalesDB.Users DISABLE GO –Enable Index ALTER INDEX [IX_Users_UserID] SalesDB.Users REBUILD Here are some resources to help you find a solution: Some bulk loading speed comparisons Use SqlBulkCopy to Quickly Load Data from your Client to SQL Server Optimizing Bulk Copy Performance … Read more

How to select rows for a specific date, ignoring time in SQL Server

You can remove the time component when comparing: SELECT * FROM sales WHERE CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, salesDate))) = ’11/11/2010′ Another approach is to change the select to cover all the time between the start and end of the date: SELECT * FROM sales — WHERE salesDate BETWEEN ’11/11/2010 00:00:00.00′ AND ’11/11/2010 23:59:59.999′ WHERE salesDate BETWEEN ‘2020-05-18T00:00:00.00’ … Read more