tsql

How to eliminate duplicate rows?

hi guys im back with my original query and i just have one question please (ps: I know i have to vote and regsiter and I promise I will do that today) With the following query (t-sql) I am getting the correct results, except that there are duplicates now. I have been reading up and think I can use the PARTITION BY syntax - can you plea...

LINQ TO SQL, ADO.NET Entity Framework, T-SQL

Greetings, I have a few applications/websites running with LINQ to SQL and the other day I decided to go ahead and optimize some of the queries, etc and I found that the size for variable length data types is derived from the parameter value instead of the column actual size? for example a column is defined as nvarchar(30). when I use ...

Stored Procedure - forcing execution order

I have a stored procedure that itself calls a list of other stored procedures in order: CREATE PROCEDURE [dbo].[prSuperProc] AS BEGIN EXEC [dbo].[prProc1] EXEC [dbo].[prProc2] EXEC [dbo].[prProc3] --etc END However, I sometimes have some strange results in my tables, generated by prProc2, which is dependent on the r...

Best Way to call a Web Service from TSQL

Hello, What is the best way to call a Web Service from TSQL? I would Like to write some triggers that call out to a web service. Is there a generally used best practice for this? Implementations would need to be handled in SQL Server 2005 and 2008 ...

Inverse of COALESCE

Hi all, is there a function in SQL Server 2005 that returns NULL [or a boolean value] if any of the arguments (of any type) is NULL, which would save me from writing IF a IS NULL OR b IS NULL OR c IS NULL .... ...

T-SQL Hierarchy to duplicate Dependent Objects tree view in SQL Server 2005

Hi Id like to map the calling stack from one master stored procedure through its hundreds of siblings. i can see it in the dialog, but cannot copy or print it, but couldnt trap anythiing worthwhile in proflier. do you know what sproc fills that treeview? i must be a recursive CTE that reads syscomments or information_schema.routines, bu...

Query takes time on comparing non numeric data of two tables, how to optimize it?

I have two DBs. The 1st db has CallsRecords table and 2nd db has Contacts table, both are on SQL Server 2005. Below is the sample of two tables. Contact table has 1,50,000 records CallsRecords has 75,000 records Indexes on CallsRecords: CallFrom CallTo PickUP Indexes on Contacts: PhoneNumber I am using this query to find matches ...

How to get resultset with stored procedure calls over two linked servers?

I have problems filling a temporary table with the resultset from a procedure call on a linked server, in which again a procedure on another server is called. I have a Stored Procedure sproc1 with the following code, which calls another procedure sproc2 on a linked server. SET @sqlCommand = 'INSERT INTO #tblTemp ( ModuleID, ParamID) ' ...

SQL Server ':setvar' Error

I am trying to create some script variables in T-SQL as follows: /* Deployment script for MesProduction_Preloaded_KLM_MesSap */ GO SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON; SET NUMERIC_ROUNDABORT OFF; GO :setvar DatabaseName "MesProduction_...

Flush SQL Management Studio Message Buffer

When I use PRINT statements to debug and monitor long running queries in SQL Server Management Studio, I notice that the output does not appear in the Messages tab right away. Is there a way to flush output to the message tab, or to set up the environment so that it doesn't buffer messages? ...

How to select all records from one table that do not exist in another table?

table1 (id, name) table2 (id, name) Query: SELECT name FROM table2 -- that are not in table1 already ...

Handling Datetime with decimal '2010-02-14 20:18:58.313000000'

In SQL Server I have some textual data in varchar fields I am trying to convert to datetime's. The funny thing is this data at some point was in a datetime field, exported to flat file, and now I am reimporting it. The problem is it is in this format 2010-02-14 20:18:58.313000000 and the conversion to datetime fails. I have no idea ho...

"select * into table" Will it work for inserting data into existing table

I am trying to insert data from one of my existing table into another existing table. Is it possible to insert data into any existing table using select * into query. I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union...

Exporting query results to a file on the fly

Hi all, I need to export the results of a query to a csv file and put the file on a network shared folder. Is it possible to achieve this within a stored procedure? If yes, comes yet another constraint: can I achieve this without sysadmin privileges, aka without using xp_cmdshell + BCP utility? If no to 2., does the caller have to h...

Rebuild all XML indexes in SQL Server 2005 using T-SQL

How can I use T-SQL to force a rebuild / reindex of all XML indexes in a SQL Server 2005 database? I only have Management Studio Express locally so I don't have the luxury of any UI options such as the "Rebuild Indexes" dialog that I know exists in SQL Server 2008. ...

TSQL Query: Escaping Special Characters

Hello all, I am trying to escape special characters in a TSQL query. I have done this before: SELECT columns FROM table WHERE column LIKE '%\%%' ESCAPE '\' And it has worked. Now I have tried to do this now: UPDATE match SET rule_name='31' ESCAPE '\' But it has failed. I know none of the vlaues have a \ but it should still work. I...

Is file_get_contents multi line?

Hello all, Does file_get_contents maintain line breaks? I thought it did but I have tried this: if($conn){ $tsql = file_get_contents('scripts/CreateTables/SLR05_MATCH_CREATETABLES.sql'); $row = sqlsrv_query($conn, $tsql); print_r(sqlsrv_errors()); } The errors I get is that SQL Server complains that there is incorrect...

Sql Server - How to calculate the size of some rows from each table in a database?

How can i calculate the size of only some rows for each table? For example, with the code: EXEC sp_spaceused 'myTable' you obtain the size of all rows, however i want to calculate the size not of one single table, but all of them, and using the same clause for each one, something like this in pseudo-code: foreach(Table myTable in Da...

Anyway to get a value similar to @@ROWCOUNT when TOP is used?

If I have a SQL statement such as: SELECT TOP 5 * FROM Person WHERE Name LIKE 'Sm%' ORDER BY ID DESC PRINT @@ROWCOUNT -- shows '5' Is there anyway to get a value like @@ROWCOUNT that is the actual count of all of the rows that match the query without re-issuing the query again sans the TOP 5? The actual problem is a much more com...

Translate SQL to LINQ query - group/join/filter

I have the following query: SELECT S.[FlowOrder], S.[DESCRIPTION], COUNT(I.ID) FROM WorkFlowStatus AS S INNER JOIN Item AS I ON S.ID = I.StatusID WHERE I.Installation = '1' GROUP BY S.[Description], S.[FlowOrder] ORDER BY S.[FlowOrder] Which gives me the count of an item, grouped by a foreign key to workflow, outputting the descri...