How to automatically generate unique id in SQL like UID12345678?

The only viable solution in my opinion is to use an ID INT IDENTITY(1,1) column to get SQL Server to handle the automatic increment of your numeric value a computed, persisted column to convert that numeric value to the value you need So try this: CREATE TABLE dbo.tblUsers (ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY … Read more

Why does the Execution Plan include a user-defined function call for a computed column that is persisted?

The reason is that the query optimizer does not do a very good job at costing user-defined functions. It decides, in some cases, that it would be cheaper to completely re-evaluate the function for each row, rather than incur the disk reads that might be necessary otherwise. SQL Server’s costing model does not inspect the … Read more

How to concatenate all columns in a select with SQL Server

Any number of columns for a given tablename; If you need column names wrapped with <text> DECLARE @s VARCHAR(500) SELECT @s = ISNULL(@s+’, ‘,”) + c.name FROM sys.all_columns c join sys.tables t ON c.object_id = t.object_id WHERE t.name=”YourTableName” SELECT ‘<text>’ + @s + ‘</text>’ SQL Fiddle Example here — RESULTS <text>col1, col2, col3,…</text> If you … Read more

replace NULL values with latest non-NULL value in resultset series (SQL Server 2008 R2)

You can try the following: * Updated ** — Test Data DECLARE @YourTable TABLE(Product INT, Timestamp DATETIME, Price NUMERIC(16,4)) INSERT INTO @YourTable SELECT 5678, ‘20080101 12:00:00’, 12.34 UNION ALL SELECT 5678, ‘20080101 12:01:00’, NULL UNION ALL SELECT 5678, ‘20080101 12:02:00’, NULL UNION ALL SELECT 5678, ‘20080101 12:03:00’, 23.45 UNION ALL SELECT 5678, ‘20080101 12:04:00’, NULL … Read more

How do I find the data directory for a SQL Server instance?

It depends on whether default path is set for data and log files or not. If the path is set explicitly at Properties => Database Settings => Database default locations then SQL server stores it at Software\Microsoft\MSSQLServer\MSSQLServer in DefaultData and DefaultLog values. However, if these parameters aren’t set explicitly, SQL server uses Data and Log … Read more