tsql

SQL: Query to return inaccurate data entry

My company needs to write a query that determines when data is being entered inaccurately. We have a table in our db called "ProductChanges". It looks (something) like this - Product User ChangeDate OldValue NewValue 2344 John 24/01/10 10:00 2344 4324 6435 Greg 28/01/10 13:30 ...

T-Sql Modify Insert SProc To Update If Exists.

Scenario I have a stored procedure written in T-Sql that I use to insert data into a table as XML. Since the data gets updated regularly, I want the rows to be updated if they already exist (Aside from when the application is first run, they will always exist). Question Below is the code of my Insert Sproc, however I cannot seem to wo...

Performance for delete query in sql

I have a table with 200,000 records. I want delete some data like below : DELETE FROM Table Where IdColumn IN ( SelectedID ) SelectedID fill with my desirable data identities that contains 5000 records. there are 2 approach: 1- I insert 20,000 identity as SelectedID that contains identities of desired 5000 records. 2- I insert only ...

Select * into SqlserverTable2 from SqlExpressTable1, can this be done

Can I Select * into a Sqlserver Table from a SqlExpress Table the way I can from a Sqlserver Table to a Sqlserver Table using Select * into Table2 from Table1 If so, what is the syntax? ...

SQL Server JOIN - displaying two joined values in one query that map to one other table?

Here is an example of the data set I am working with. I am trying to write a query that will allow me to request a row from the items table, and display the User Name from the users table in place of the user ids: Table 1 - users table User ID | User Name -------------------- 12 | Fred 13 | Ted 14 | Ned Table 2 - items...

T-SQL: How can you create a table with SELECT?

In oracle, you can issue: create table foo as select * from bar; What is the equivalent T-SQL statement? ...

How do I output a table to a txt file using tsql?

How do I output a table to a txt file using tsql? I don't want to use DTS or SSIS in this instance. ...

What is the Correct Way To Have An Adjustments View Between Two Tables in SQL

I'm looking for a way to non-destructively alter data. I have one table that has data that shouldn't be edited - it is the master data. I would like to have user edits on this data, but in order to maintain the integrity of the master data I've created a second table that is a mirror of the structure of the master table. My thinking i...

How to get unique set of rows from SQL where uniqueness is defined by 2 columns?

If I have a table as follows: ID | Name | Category | Parent | URL | LastModified If two rows have the same Name and parent, then they are not unique. How do I get the unique set of rows in this case (but return more than the columns that make them unique)? So, for more details: This is a corporate keywords table, where keywords are o...

SQL FOR XML Help

Prob an easy question, but I am new to forming XML in SQL 2005, but What would be the best FOR XML SQL statement to use to form the XML seen below from a table that looks like this? Column1 Column2 ------------------------ Baseball Football Cricket Polo Swim Beach Desired XML output: <Category Nam...

Check if I can execute some sql-command

I'm using ADO .NET and MS SQL Server 2008. I have a connection object to a server and a command: SqlConnection conn = /* my connection*/; string cmd = "some_sql_command"; I want to check if SQL Server can execute cmd. I don't want to execute cmd, but I want to know If SQL Server can execute it. cmd can be any single SQL statement, i...

Get top one record of same FK on date difference.

I need to get top 1 record from same customer inserted within one hour. If record is inserted after one hour then don't need that one. Please see following table. This is just a sample of 1000s of records. I am using SQL Server 2005. ...

Indexed Views And Scalar UDFs

Can UDFs be used inside index views or does this break the SCHEMABINDING logic? For example if in the where clause of the view I had something like this WHERE UserCanSeePost(UserId,PostId) This UDF would then do a lookup on another table to check some restrictions. I'm guessing this cant be done in indexed views? ...

Cannot create a stop list in SQl 2008

I'm new to the stop list functionality in SQL 2008 and seem to be missing something obvious. I'm trying to exectue the following statement: CREATE FULLTEXT STOPLIST myStoplist3 FROM SYSTEM STOPLIST; GO and I'm getting the following error: Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'STOPLIST'. Any ideas? ...

SQL queries running slowly or stuck after DBCC DBReindex or Alter Index

All, SQL 2005 sp3, database is about 70gb in size. Once in a while when I reindex all of my indexes in all of my tables, the front end seems to freeze up or run very slowly. These are queries coming from the front end, not stored procedures in sql server. The front end is using JTDS JDBC connection to access the SQL Server. If we stop a...

Check if Database Exists Before Creating

This seems pretty trivial, but it is now frustrating me. I am using C# with SQL Server 2005 Express. I am using the following code. I want to check if a database exists before creating it. However, the integer returned is -1 and this is how MSDN defines what ExecuteNonQuery() will return as well. Right now, the database does exist but ...

SQL trigger on Truncate

How to Fire a trigger when you do TRUNCATE (instead deleted) in MSSQL ...

Using System Views

This is a general question that keeps coming to my mind. Is there any major disadvantage in using the SQL Server 2000 system tables in SQL Server 2008? It is a good practice to use system views, but in simple words, Why? ...

in sql between @startdate and @enddate does not compare time part ? How to compare time parts of date.

I have query like this SELECT AVAILABILITY_DATE_TIME FROM APPT_PROVIDER_AVAILABILITY WHERE AVAILABILITY_DATE_TIME between @START_DATE AND @END_DATE suppose i have if @startdate = '2/11/2010 11:31:00 AM' and @enddate = '2/11/2010 11:56:00 AM' then difference is zero its ignoring time part . If you can provide query ...

select records which are repeated twice!

I have a table in SQL Server with these records : ID Date Time -- --------- ---- 1 09/05/02 6:00 2 09/05/02 8:00 3 09/05/03 6:00 4 09/05/04 8:00 5 09/05/04 6:00 I would like to select those ID who have records in a day which are repeated twice or multiple of two. How wo...