sql-server-2005

Possible to get PrimayKey IDs back after a SQL BulkCopy?

Hi I am using C# and using SqlBulkCopy. I have a problem though. I need to do a mass insert into one table then another mass insert into another table. These 2 have a PK/FK relationship. Table A Field1 -PK auto incrementing (easy to do SqlBulkCopy as straight forward) Table B Field1 -PK/FK - This field makes the relationship and is a...

Get top 'n' records by report_id

I have a simple view in my MSSQL database. It consists of the following fields: report_id INT ym VARCHAR -- YYYY-MM keyword VARCHAR(MAX) visits INT I can easily get the top 10 keyword hits with the following query: SELECT TOP 10 * FROM top_keywords WHERE ym BETWEEN '2010-05' AND '2010-05' ORDER BY visits DESC Now where it gets tric...

How to do insert one row from one table to another table(what has less columns)?

Edit Ok I solved all my problems but one. Is it possible to have a hardcoded value insert as well. Like I said I am trying to insert one row(some of its columns) into another table that has about 80% of the same columns however the remaining ones hard non null columns and need some value to be inserted into them. I am wondering can I s...

Exclude records matching subquery

There is probably an obvious answer to this question, but I am having one heck of a time getting anywhere with it. Consider the query: SELECT * FROM reports AS r JOIN reportvalues AS rv ON rv.report_id = r.report_id JOIN metrics AS m ON m.metric_id = rv.metric_id WHERE r.report_id NOT IN( SELECT DISTINCT report_id FROM exclude...

Group keywords by site

I am finding a lot of useful help here today, and I really appreciate it. This should be the last one for the day: I have a list of the top 10 keywords per site, sorted by visits, by date. The records need to be sorted as follows (excuse the formatting): 2010-05 2010-04 site1.com keyword1 apples wine keyword1 visits ...

How to define ENUM in SQL Server 2005?

Possible Duplicate: Does SQL Server 2005 have an equivalent to MySqls ENUM data type? Is there any way to define ENUM in SQL Server 2005? I have fixed values which I need to use in procedures and functions. ...

Disable Primary Key and Re-Enable After SQL Bulk Insert

I am about to run a massive data insert into my DB. I have managed to work out how to enable and rebuild non-clustered indexes on my tables but I also want to disable/enable primary keys as I believe this will speed up the insertion process. NOTE: This is over numerous tables and so I assume I need some loop to get the primary key info...

Synchronise structure of multiple database

Hi, I have 2 database: production and to test. Production should have the same fields and tables like testdatabase. is there any software, or script to show difference between this two databases? ...

How do I create a Pivot query for this simple case?

I have a very simple query that I need pivoted, but I haven't got a clue how to turn it into a pivot table from what I've read in the literature. Everything I've read involves hard-coded columns or is too complex. It has to be simpler, or I just can't grasp CTE's. The query is a list of user names and clients they have access to. So I...

SQL Server 2005: Partial Filegroup restore on QA database

Okay, I am pretty sure at this point that this cannot be done, but here goes: I have a production SQL Server 2005 database with two filegroups, PRIMARY and SECONDARY. PRIMARY consists of about 2 gigs of typical database data. SECONDARY consists of 15 gigs of archived data that no longer changes. I would like to create a QA database in ...

'Timeout Expired' error when buffer cache clean

I am going to sum up my problem first and then offer massive details and what I have already tried. Summary: I have an internal winform app that uses Linq 2 Sql to connect to a local SQL Express database. Each user has there own DB and the DB stay in sync through Merge Replication with a Central DB. All DB's are SQL 2005(sp2or3). We...

SQL Design Question regarding schema and if Name value pair is the best solution

I am having a small problem trying to decide on database schema for a current project. I am by no means a DBA. The application parses through a file based on user input and enters that data in the database. The number of fields that can be parsed is between 1 and 42 at the current moment. The current design of the database is entirely ...

SQL server 2005 FOR XML RAW performance/behaviour issue.

I have a really weird problem that baffled even seasoned DBA's here where I work. I'm trying to troubleshoot performance problems with this legacy app that was originally developed with VB/SQL 2000. Most clients using this app with SQL 2000 don't have any performance issues. Some clients have SQL 2005 and have really bad performance exe...

Single SQL Server Result Set from Query

Hi Please advise on how to merge two results in to one using SQL Server 2005. I have the situation where an Account can have up to two Settlement Instructions and this has been modeled like so: The slim-ed down schema: Account --------------------- Id AccountName PrimarySettlementId (nullable) AlternateSettlementId (nullable) Settl...

SQL Drop Index on different Database

While trying to optimize SQL scripts, I was recommended to add indexes. What is the easiest way to specify what Database the index should be on? IF EXISTS (SELECT * FROM sysindexes WHERE NAME = 'idx_TableA') DROP INDEX TableA.idx_TableA IF EXISTS (SELECT * FROM sysindexes WHERE NAME = 'idx_TableB') DROP INDEX TableB.idx_Tabl...

Optimizing T-SQL where an array would be nice

Alright, first you'll need to grab a barf bag. I've been tasked with optimizing several old stored procedures in our database. This SP does the following: 1) cursor loops through a series of "buildings" 2) cursor loops through a week, Sunday-Saturday 3) has a huge set of IF blocks that are responsible for counting how many Objects of ...

T-SQL 2005 - Divide by zero error encountered

Hi, I am trying to get some percentage data from a stored procedure using code similar to the line below. Obviously this is going to cause a (Divide by zero error encountered) problem when base.[XXX_DataSetB] returns 0 which may happen some of the time. Does anyone know how i can deal with this in the most efficient manner? Note: Ther...

SQL to return a table based on string input

Hello everyone, I am a new with T-SQL. So, please help me to write the sql. I have table Price (Code column is primary column): Code Value A1 234 A2 525 A3 566 I will input a string and the sql need to return a table. Ex1: input 'A2' -> return: Code Value A2 525 Ex2: input 'A1 A3' -> return: Code ...

Run sql script from console and not from query analyzer in SQL server

I have written an sql script for updating a database that runs in SQL server 2005. I want to make those changes to the production DB server but I dont want to run the query from the query analyzer. Is there a way to run the sql script from a console? ...

Search book by title, and author

I got a table with columns: author firstname, author lastname, and booktitle Multiple users are inserting in the database, through an import, and I'd like to avoid duplicates. So I'm trying to do something like this: I have a record in the db: First Name: "Isaac" Last Name: "Assimov" Title: "I, Robot" If the user tries to add it again, ...