tsql

How are these tasks done in SQL?

I have a table, and there is no column which stores a field of when the record/row was added. How can I get the latest entry into this table? There would be two cases in this: Loop through entire table and get the largest ID, if a numeric ID is being used as the identifier. But this would be very inefficient for a large table. If a ra...

How can I return a list of values from a stored procedure?

Well I am calling a stored procedure from another stored procedure and I need it to return something like an array.How can I do it? ...

Create dummy data with WHILE

Hi, I am pretty new in TSQL, I try to insert some dummy data inside my table using a WHILE, but it run really really slow. I was thinking maybe I am writing not properly the code, could yo please have a look and confirm it? thanks -- Insert dummy data DECLARE @i int, @Content int; SET @i = 5001; WHILE @i > 5000 ...

Send mail by SQL server 2005

i am not getting this store procedure , can you tell where is it "master..xp_sendmail" i have send the mail when any insertion take place in particular table like "Emp" ...

SQL 2005 - Linked Server to Oracle Queries Extremely Slow

On my SQL 2005 server, I have a linked server connecting to Oracle via the OraOLEDB.Oracle provider. If I run a query through the 4 part identifier like so: SELECT * FROM [SERVER]...[TABLE] WHERE COLUMN = 12345 It takes over a minute to complete. If I run the same query like so: SELECT * FROM OPENQUERY(SERVER, 'SELECT * FROM TABLE ...

Password salting in SQL Server

I have a project to start salting passwords. In order to avoid breaking our existing applications, this will require performing the hash inside a stored procedure. Since I'm a total newbie on this topic, can you tell me where to get started. ...

Why is this (non-correlated) subquery causing such problems?

I've got a large query where a simple subquery optimization dropped it from 8 minutes down to 20 seconds. I'm not sure I understand why the optimization had such a drastic effect. In essence, here's the problem part: SELECT (bunch of stuff) FROM a LEFT OUTER JOIN b ON a.ID = b.a LEFT OUTER JOIN c ON b.ID = c.b ... ... I...

return value from stored procedure

Hello I have a stored procedure like this: Select @totalMoney*@bonusPercent/100 How i can return value from procedure and assign it to the varible? i mean something like: SET @myVarible = EXEC MyStoredProcedure @param1, @param1; ...

How to set/get/return parameters from dynamic executed query

DECLARE @T INT EXEC('SELECT @T = count(user_id) FROM ( .. .. .. )') PRINT @T I guess the example is sufficient to explain what i am trying to do, i create dynamic query and want to access its count outside the exec statement. I am using SQL 2000. How can this be achieved ? ...

T-SQL: SELECT INTO sparse table?

I am migrating a large quantity of mostly empty tables into SQL Server 2008. The tables are vertical partitions of one big logical table. Problem is this logical table has more than 1024 columns. Given that most of the fields are null, I plan to use a sparse table. For all of my tables so far I have been using SELECT...INTO, which ha...

TSQL: Get Last Queries Ran

Is there a way to get the SQL text for the last few queries? I am using Microsoft SQL Server 2005 ...

Tsql Union Query is breaking sorting in my asp.net gridview

Hello All, I have a stored procedure that works fine on its own. A recent requirement has made me think a Union query will accomplish what I need. Here is the working version. It's uses the ROW_NUMBER() to accomplish paging and sorting correctly SELECT x.TicketID, x.TicketNumber, x.AccountID, x.SkillID FROM ( SELECT ROW_NUMBER() O...

SQL Server 2008 - Concatenate String

Hi All, I have the need to take the results of a query per line item and build a semi-colon delimited list of materials that make up that item. Schema Makeup: Tables: LineItems (Unique Item Listing) LineItems_Materials (Many to Many) Materials (Unique Material Listing) Line Items: ID|LineItem 1|'1A.1' LineItems_Materials: ID|LineIt...

selecting data between a certain date range where date range is a varchar

i have a varchar reporttime that has the date stored. dont ask me why it is a varchar i am doing this to get data between two dates: select rowid from batchinfo where CONVERT(DATE, reporttime, 103) between '2010-07-01' and '2010-07-31' and it is not working what am i doing wrong? btw this is what the data looks like: rowid da...

My T-SQL query needs additional SUM's from an additional table...

The following is my current query. It works the way i want it to. However, it needs some improvements so that i can 1) retrieve the SUM's of the money columns in my 'expenses' table and 2) add a WHERE clause (or HAVING?) so that all of the SUM's are year-to-date values, e.g. any amounts prior to Jan. 1, 2010 will not be totaled by the SU...

Index Key Column VS Index Included Column

Hi there Can someone explain this two - Index Key Column VS Index Included Column? Currently, I have an index that has 4 Index Key Column and 0 Included Column. Thanks ...

How to take differentiate this value in TSQL?

Hi there We have a simplified table like this: TYPE; VALUE AA; 10 BB; 7 CC; 12 I want to do calculation based on TYPE AA & BB so 10 - 7 = 3 How do I achieve this? Thanks ...

Using delimiters to split column in T-SQL while skipping delimiters

OK the question title is vague, but here's the problem. I have a list of filenames in the first column of a table called Files that I want to parse data from that are underscore delimited, however sometimes I want to skip the underscore. Every filename is in the form of: distance1000_7_13_2010_1_13PM_AveryDennisonAD_2300008_10S_Lock.csv...

SQL Server Select on Different column

Hi All, I want to select a value based on different column in SQL Server, say Result = Isnull(A.OUT1,'')<>'' then select A.OUT1 Isnull(A.OUT2,'')<>'' then select A.OUT2 Isnull(A.OUT3,'')<>'' then select A.OUT3 How to form query to get the Result? ...

Returning first instance of value across multiple tables (and returning associated columns)

I am looking to return the following information for each contact contained within a set of tables: Contact_id | first_Event_date | first_Event_id | event_type id123456 | 01/12/2007 | eveid123456 | table1 id456455 | 05/06/1999 | eveid456585 | table4 Where the data reflect...