tsql

SQL Help: Find which columns caused the mismatch

The query below selects the rows from table_1 which do not exist in table_2 based on values in the 4 columns. The mismatch could be due to one or more columns. I would like to extend the query to tell me which column(s) had the mismatched values, either by showing the column name or its value. I can do this in a cursor but prefer to do...

Sql server takes too long time for update query to execute for few rows

Hi Guys, UPDATE table1 SET col1='True' This query takes more than 30 secs for about 6000 records. Why is it so slow? ...

inner join on null value

I'm not sure if i made a mistake in logic. If i have a query and i do an inner join with a null value would i always get no results or will it ignore the join and succeed? example user { id PK, name NVARCHAR NOT NULL, banStatus nullable reference } if i write and u.banStatus i will receive no rows? select * from user as u join banst...

Bulk DELETE on SQL Server 2008 (Is there anything like Bulk Copy (bcp) for delete data?)

Is there any solution for bulk delete in SQL Server? I can't use TRUNCATE because I want to use WHERE for limiting the rows in action. Is there anything like Bulk Copy (bcp) for delete data? ...

T-SQL View to select FULL OUTER JOIN and add additional rows on "A" based on "B" table

Hi, I hope someone can help me. I need to solve a problem with a view that get me the correct result based in the following scenario. I have two tables: Language and ReportDescription Language Id Tag Description ---- ---- ---------------- 1 en English 2 de German 3 fr French 4 it Italian ReportDe...

writing a transaction in t-sql and error handling

Hi Do u think there is a better way to write a transaction in t-sql? Is there a better approach that improves maintainability and performance of the application that uses this transaction? -- Description: Insert email Receiver under specified subject -- ============================================= ALTER PROCEDURE [Contact].[Receiver_...

How can i store the result of a comparison into a variable

I want to print a simple statement print (1=1), i expect the result to be TRUE or 1 but sql server tell me: Incorrect syntax near '='. why is that? Same will happen for a statement like that declare @test bit set @test = (1=1) in summary how can i "see" what is returned from a comparison without using an IF statement Update: The re...

How do I count occurrences by day in SQL?

Newbie SQL question here --> I've got an Occurrences table that contains a row for each time a user did something. A user can do the thing multiple times per day. It looks like this: Date Username ------ -------- 1/1/9 User1 1/1/9 User1 1/1/9 User2 1/2/9 User1 1/2/9 User3 1/3/9 User1 1/3/9 User1 1/...

Find the user with the most contact

Users send messages to each others message boxes, how to find the user who knows the most other users (receive AND send??). I have SQL Server. Table: USER_ID USER_NAME USER_GROUP MESSAGE_ID MESSAGE_MESSAGE MESSAGE_FROM_USER M_ESSAGE_TO_USER A user can send and receive, so who knows the most other users (by send AND receive??) ...

Re-writing this sql query

Hi, I have written an SQL query that gets unread messages but I think I can improve the code (speed and readability). The first select is for COUNT function, the second is for grouping messages according to conversation_id, and the final nested select is for selecting last messages. Please give me suggestions. Thanks in advance. SELEC...

SQL - select distinct records in one field with highest records from another field

In a scenario where I have a table like so: int id (PK) int staff_id int skill_id bit mainskill I want to select only ONE record for each staff member (represented by staff_id) listing their main skill as represented by a (1) in mainskill. If no main skill is present, I want to return any of the skill records for that staff member. Fo...

dynamically create table statement in sql

how to dynamically create a table with same columns as that of previous table. in sql ...

Datetime display using month name in T-SQL

Hi all There is a field which is date type in a table. The format of this field is mm/dd/yy. Is there any way to convert it to dd-Month name-yy? Best Regards, ...

Convert Byte Array to string using TransactSQL

Hi, We are store string values in a database using varBinary type using c# and BinaryFormatter.We convert to byte array and then we save to DataBase public static byte[] ToBytes(stringvalue) { if (value == null) return null; byte[] inMemoryBytes; using (MemoryStream inMemoryData = new MemoryStream()) { new BinaryFormat...

counting number of entries in a month

Hi, I have an order table, and it has a datetime column called order_date. I want to count the number of orders for each month in 2009. How would I do that? ...

Calling SQL Scrips from a sql script!

I am just wondering if it is possible to call multiple SQL scripts from a single startup.sql script. i.e. StartUp.sql calls: CreateDatabase.sql CreateStoreProcedureFirst.sql CreateStoreProcedureSecond.sql InsertDummyValues.sql otherscripts.sql... etc At the moment I am loading and running each file one at a time.. there are more scri...

Compare stored procedures across multiple databases (SQL Server)

SQL Gurus -- Our architecture consists of multiple customer databases to a common codebase. When we deploy database changes, the scripts must be run agianst each database. Because of deployment issues, there have come times when our stored procedures became out of sync with one another. I would like to create a script to return these m...

SQL Query to aggregate DateDiff greater than given value

I have a logging table used for device “heartbeats”. I have these network devices that check-in/heartbeat with the server every 10 minutes. We are wanting statistics on when they miss their scheduled check-in time. I have a query that can do this on a per-device basis, but I need it to be modified to handle across all devices. The he...

SQL - Counting and Summarizing (in ranges) the number of instances of a given transaction

I've got an Occurrences table that contains one row for each time a user took an action. A user take an action multiple times per day. It looks like this: Date Username ------ -------- 1/1/9 User1 1/1/9 User1 1/1/9 User2 1/2/9 User1 1/2/9 User3 1/3/9 User1 1/3/9 User1 1/3/9 User1 1/3/9 User2...

SQL Query execution shortcut OR logic?

I have three tables: SmallTable (id int, flag1 bit, flag2 bit) JoinTable (SmallTableID int, BigTableID int) BigTable (id int, text1 nvarchar(100), otherstuff...) SmallTable has, at most, a few dozen records. BigTable has a few million, and is actually a view that UNIONS a table in this database with a table in another databas...