tsql

Can you modify SQL DB schema in a transaction to know if all changes were applied?

As part of my (new) database version control methodology, I'm writing a "change script" and want the change script to insert a new row into the SchemaChangeLog table if the script is executed successfully, or to reverse changes if any single change in the script fails. Is it possible to do schema changes in a transaction and only if it ...

convert function from Access SQL to T-SQL 2005

Can someone please convert this access sql function for me to work in t-sql 2005. I am tring to take the selling price minus the cost as one number. And divide that by the original selling price to produce a second number Thanks :) =IIf([Selling Price]=0,0,([Selling Price]-Nz([Cost]))/[Selling Price]) IIRC it should be something ...

SQL Server 2008 - Conditional Query

Hello, SQL is not one of my strong suits. I have a SQL Server 2008 database. This database has a stored procedure that takes in eight int parameters. For the sake of keeping this question focused, I will use one of these parameters for reference: @isActive int Each of these int parameters will be -1, 0, or 1. -1 means "Unknown" or "D...

After Delete Trigger Fires Only After Delete?

I thought "after delete" meant that the trigger is not fired until after the delete has already taken place, but here is my situation... I made 3, nearly identical SQL CLR after delete triggers in C#, which worked beautifully for about a month. Suddenly, one of the three stopped working while an automated delete tool was run on it. By ...

TSQL to Map User to Database

So I'm not able to user enterprise manager to do this... If I was I wouldn't even be asking this question. So I'm wondering if there is a way through TSQL to execute a command that maps a User to a particular Database and grants them 'owner' permissions. Thanks... ...

@@TRANCOUNT and a current connection

Assume I connect to SQL server 2008 via SQL Server Management Studio ( SSMS ) and open new window W1 by clicking on New Query tab and write the following inside W1: BEGIN TRANSACTION; If I execute this statement 5 times, and then write (inside W1) SELECT @@TRANCOUNT; , then the value returned will be 5. But if I open another window...

SQL Server: position based on marks

I am using SQL Server 2008. I have a Student table in which there are following fields: 1. StudentId, 2. StudentName, 3. Marks . I want to get a resultset in which there should be a column named “Position”. Something like “Select StudentId,StudentName,Marks, as Position from Student...” so that, depending on the marks a student scored, i...

How to execute big SQL files on SQL Server?

I have about 50 T-SQL files, some of them are 30MB but some of them are 700MB. I thought on executing them manually, but if the file is bigger than 10MB it throws an out of memory exception on the SQL Server Management Studio. Any ideas? ...

TSql Lookup function

I have a bunch of dimension tables that have unique ID and Name fields. I need a T-SQL function that returns an ID when passed a table name and a value for the name field. I'm guessing the function would build a little query then execute it? Performance isn't an issue since this is a one time ETL thing. ...

In SQL Azure how what script can I use to create a read only user

I would like to create an Sql Azure user and grant her readonly access on a handful of DBs, what script can I use to achieve this? ...

Linq-to-SQL Grouping not ordering correctly

Hi can someone help me convert this tsql statement into c# linq2sql? select [series] from table group by [series] order by max([date]) desc This is what i have so far - the list is not in correct order.. var x = from c in db.Table orderby c.Date descending group c by c.Series into ...

Getting the most recent entry per group in a select statement

I have 3 tables to join to get table1.code, table1.series, table2.entry_date, table3.title1 and I'm trying to get the most recent non null table3.title1 grouped by table1.code and table1.series. select table1.code, table1.series, max(table2.entry_date), table3.Title1 from table3 INNER JOIN table2 ON table3.ID = table2....

SQL JOIN with two or more tables as output - most efficient way?

I have an SQL query that executes a LEFT JOIN on another table, then outputs all results that could be coupled into a designated table. I then have a second SQL query that executes the LEFT JOIN again, then outputs the results that could not be coupled to a designated table. In code, this is something like: INSERT INTO coupledrecords SE...

TSQL Help (SQL Server 2005)

I have been playing around with a quite complex SQL Statement for a few days, and have gotten most of it working correctly. I am having trouble with one last part, and was wondering if anyone could shed some light on the issue, as I have no idea why it isnt working: INSERT INTO ExistingClientsAccounts_IMPORT SELECT DISTINCT cca.Accoun...

IDENTITY_INSERT ON inside of cursor does not allow inserted id

I am trying to set some id's for a bunch of rows in a database where the id column is an identity. I've created a cursor to loop through the rows and update the ids with incrementing negative numbers (-1,-2,-3 etc). When I updated just one row turning on the IDENTITY_INSERT it worked fine but as soon as I try and use it in a cursor, it...

How to convert a "dd/mm/yyyy" string to datetime in SQL Server?

I tried this SELECT convert(datetime, '23/07/2009', 111) but got this error The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. However SELECT convert(datetime, '07/23/2009', 111) is OK though How to fix the 1st one ? ...

Add multiple columns in select query

Scenario is like: SomeTable { A int, B int, C int } I need to select and add with formula: A + 25%B + 50%C. If in decimal, round upto next full digit and display an asterisk and a spcae preciding the sum. Each column needs to be rounded off before adding. Suppose data is 10 16 12 14 15 19 Select should return: 20, 28 *. I am thin...

Optimal way to convert to date

I have legacy system where all date fields are maintained in YMD format. Example: 20101123 this is date: 11/23/2010 I'm looking for most optimal way to convert from number to date field. Here is what I came up with: declare @ymd int set @ymd = 20101122 select @ymd, convert(datetime, cast(@ymd as varchar(100)), 112) This is prett...

I'm looking for a reliable way to verify T-SQL stored procedures. Anybody got one?

Hi all-- We're upgrading from SQL Server 2005 to 2008. Almost every database in the 2005 instance is set to 2000 compatibility mode, but we're jumping to 2008. Our testing is complete, but what we've learned is that we need to get faster at it. I've discovered some stored procedures that either SELECT data from missing tables or try to...

Is it possible to run an alter on all my database objects to test them

I'm in the course of doing some schema migrations, and would like to know if it's possible or advisable to run every stored procedure, view and function in my database as an alter statement to "compile" them all to make sure nothing is completely broken. ...