tsql

Updating row of table Using data from multiple columns of another table.

I have following table which I want to update using another table, given below. I want to update Null values of above given table using following table on the basis of ProductId. The updated table should be like this. I have mentioned ProductId in these table just for example. I don't know exact ProductId. It could be any ProductId....

TSQL: Any benefits for explicitly specifying NVARCHAR in a string?

When you add pass a new job_type to sys.sp_cdc_add_job @job_type, (which is of type nvarchar(20)) You can pass the argument as either N'cleanup' cleanup Are there any reasons or benefits to use the former syntax using N' to pass the argument to stored procedures? ...

Can calculated column be used in another calculated column?

SELECT ExchangeRatePrice = CASE pp.Price WHEN NULL THEN 0 ELSE (CASE WHEN c.CurrencyId = 1 THEN pp.Price ELSE CONVERT(DECIMAL(9, 2), (pp.Price * c.ExchangeRate)) END) END , price as OriginalPriceInDB, 10 * Price as CalculatedPrice, c.currencyid as Currency FROM ProductPrice pp, currency c I w...

parser datetime via tsql

Update List set Date = "2009-07-21T19:00:40" sql server doesn't recognize this format. is there a conversion function ...

Weighted average in T-SQL (like Excel's SUMPRODUCT)

I am looking for a way to derive a weighted average from two rows of data with the same number of columns, where the average is as follows (borrowing Excel notation): (A1*B1)+(A2*B2)+...+(An*Bn)/SUM(A1:An) The first part reflects the same functionality as Excel's SUMPRODUCT() function. My catch is that I need to dynamically specify...

Can you SELECT WHERE something's LIKE and IN at the same time?

I need to write what I'd like to call a Valley Girl-query. I need to SELECT something that's LIKE IN - something like this: SELECT * FROM Table1 WHERE Name LIKE IN ( SELECT Name FROM Table2 ) The reason for this is I've got a table full of company names, but they're not exactly the same - f.ex. in Table1 it might say "Chrysler Gr...

How does BULK INSERT work internally ?

Hi , Could someone please explain how does BULK INSERT internally work and why is it much faster than the normal INSERT operations ? Regards, Shishir. ...

What is a batch?

In Transact-SQL, a batch is a set of SQL statements submitted together and executed as a group, one after the other. Batches can be stored in command files. Is an *.sql file containing several SQL statements considered a batch? What else do we consider a batch? ...

replacing special characters in SQL 2000

Hi, how can I replace special characters in my data files (special characters such as bullet points, percent sign, hyphen etc) ? Thanks ...

The Return statement

Hi Assuming A.sql contains the following code, then second Select query won’t be executed due to Return statement: select * from Films; return; select * from Films; If A.sql was called inside a stored procedure SP1 or batch B1, then RETURN would transfer control back to SP1 or B1, respectively. But assuming A.sql isn’t called f...

linq sql where closest to number

i have a table Id Number 1 9 2 10 3 12 4 19 5 20 select Id where Number is closest to 18 it should return row 4 which is 19 how do i write this in linq and tsql? thanks ...

Is there any library for T-SQL to turn it object oriented?

Hi all, Microsoft Ajax Library has added full object orientation to JavaScript. Is there any library, framework, component, package, etc equivalent for T-SQL? It would be very nice to write object oriented SQL scripts in MS SQL Server. Cheers, afsharm ...

How to execute the stored procs from a dynamically generated query + sql server

I have a query that is dynamically fetching the stored proc names from all the databases. Now I want to execute the stored procs and store the result in a temptable or table variable. How to do that. Here is my SP so far Declare @GetDBNames sysname Declare @DynSql nvarchar(max) Declare DBNames cursor for Select '['+name+']' from mast...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? How should I define it? declare @myList {data type} set @myList = (1,2,3,4) select * from myTable where myColumn in @myList ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = true. IsPublic is true by default ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, SalesOrderItem, SalesOrderItemBom. Each with a corresponding Version table (i.e. SalesOrderVersion, SalesOrderItemVersion, SalesOrderItemB...

How to use XML to configure a Store procedure

What I'd like to do is keep some configuration in an external XML file and my stored procedure to open this and use the settings defined in it. Is this possible? I dont want to store the XML in a table. ...

How do I insert more than 8k in an SQL Server text column using REPLICATE()?

I've got to write a test that requires large amounts of data to be stored in a text column. When I try this (insert 2 billion X characters): INSERT INTO table VALUES ( REPLICATE('X', 2000000000) ) This is what I get: SELECT *, DATALENGTH(textCol) FROM table XXXXXXXXXXXXX.... 8000 I was hoping for more than 8000. Any ideas w...

T-SQL stored procedure error

I get the following error when I execute SP in SQL Server 2008 Management Studio: Msg 208, Level 16, State 6, Procedure BackupDB, Line 36 Invalid object name 'dbo.BackupDB'. use [Master]; go alter procedure dbo.BackupDB @dbName varchar(128), @path varchar(256) as begin declare @device1 varchar(256); declare @device2 va...

What's the difference between "::" and "sys" schemas?

As far as I know, BOL exmaple on fn_trace_getinfo used to use :: instead of sys schema in the example like following From SELECT * FROM ::fn_trace_getinfo(default) To SELECT * FROM sys.fn_trace_getinfo(default) Are there any differences between those two? And what does :: mean? ...