sql-server

SQL Server - stop or break execution of a SQL script

Is there a way to immeidately stop execution of a SQL script in SQL server, like a "break" or "exit" command? I have a script that does some validation and lookups before it starts doing inserts, and I want it to stop if any of the validations or lookups fail. ...

Dynamic cursor used in a block in TSQL?

I have the following TSQL codes: -- 1. define a cursor DECLARE c_Temp CURSOR FOR SELECT name FROM employees; DECLARE @name varchar(100); -- 2. open it OPEN c_Temp; -- 3. first fetch FETCH NEXT FROM c_Temp INTO @name; WHILE @@FETCH_STATUS = 0 BEGIN print @name; FETCH NEXT FROM c_Temp INTO @name; -- fetch again in a loop END -- 4...

Why does MS SQL Mgmt Studio Express keep forgetting my passwords?

I have about had it with this tool, I check the save password box at the login dialogue but it just doesn't work. Sometimes it will for a few days, and then the password will just be gone. Nearly every time I load this thing up I have to track down the password again and type it in. Is there some password rule in the database that would ...

What are the best practices for creating indexes on multiple bit columns?

Good day, In SQL Server 2005, I have a table numerous columns, including a few boolean (bit) columns. For example, table 'Person' has columns ID and columns HasItem1, HasItem2, HasItem3, HasItem4. This table is kinda large, so I would like to create indexes to get faster search results. I know that is not I good idea to create an index...

ALTER TABLE with programmatically determined constant DEFAULT value

I am trying to add a column (MSSQL 2005) to a table (Employee) with a default constraint of a primary key of another table (Department). Then I am going to make this column a FK to that table. Essentially this will assign new employees to a base department based off the department name if no DepartmentID is provided. This does not work...

SQL Server/T-SQL via JSP: "The multi-part identifier XX.YY could not be bound"

I'm getting the error: the multi-part identifier "IC.industry" could not be bound when making this SQL query from a JSP page via JDBC: select C.company, C.shname, C.fullname, count(d_to_c.designer) from companies C left join ind_to_c IC on C.company = IC.company left join d_to_c on C.company= d_to_c.company where IC.indust...

Concurrency with Linq To Sql and ASP.NET

What is the best way to check for concurrency issues when using LINQ to SQL in an ASP.net application. In my application, I am retrieving a record from the database and displaying the fields in editable textboxes. Then the datacontext is thrown away. How should I save the entity object so that I can use L2Sql's built in concurrency fea...

Open a Table and Use the query designer for SQL 2008

In SQL Server 2005 and managing my Crystal Reporting I used to open a table (small one) and then paste in the Crystal query. Using the Query designer I would add-remove tables and fields etc using all 4 of the Query Designer windows. I understand that Open table has been replaced and I would be ok with that as I replace the SQL, but it ...

Trigger event is fired only once for Microsoft SQL Server 2005 DB if more than one rows updated?

I have a table MyTable with a trigger defined like this: ALTER TRIGGER [MyTableInsertDeleteUpdate] ON [dbo].[MyTable] AFTER INSERT,DELETE,UPDATE AS DECLARE @id int; BEGIN SELECT @id = ins.id FROM inserted ins; IF (@id IS NOT NULL) BEGIN -- insert a new record to audit table PRINT 'inserted/updated id: ' + CAS...

How can I compare two tables and delete on matching fields (not matching records)

Scenario: A sampling survey needs to be performed on membership of 20,000 individuals. Survey sample size is 3500 of the total 20000 members. All membership individuals are in table tblMember. Same survey was performed the previous year and members whom were surveyed are in tblSurvey08. Membership data can change over the year (e.g. n...

SQL Server - Individual Procedures vs. Single Procedure

Hi, In a system I'm developing I have a choice of either using a single stored procedure that does three related jobs and returns either no result, or the same result set, but sourced from two different tables. I read an article yesterday that suggested that a stored-procedure should only have one execution plan and that any procedure ...

SQL Server deadlocks between select/update or multiple selects

All of the documentation on SQL Server deadlocks talks about the scenario in which operation 1 locks resource A then attempts to access resource B and operation 2 locks resource B and attempts to access resource A. However, I quite often see deadlocks between a select and an update or even between multiple selects in some of our busy ap...

Oracle sequence but then in MS SQL Server

In Oracle there is a mechanism to generate sequence numbers e.g.; CREATE SEQUENCE supplier_seq MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 INCREMENT BY 1 CACHE 20; And then execute the statement supplier_seq.nextval to retrieve the next sequence number. How would you create the same functional...

Handling multiple records in a MS SQL trigger

Hello Stack Overflow! I am having to use triggers in MSSQL for the first time, well triggers in general. Having read around and tested this myself I realise now that a trigger fires per command and not per row inserted, deleted or updated. The entire thing is some statistics for an advertising system. Our main stat table is rather larg...

Better techniques for trimming leading zeros in SQL Server?

I've been using this for some time: SUBSTRING(str_col, PATINDEX('%[^0]%', str_col), LEN(str_col)) However recently, I've found a problem with columns with all "0" characters like '00000000' because it never finds a non-"0" character to match. An alternative technique I've seen is to use TRIM: REPLACE(LTRIM(REPLACE(str_col, '0', ' ')...

How to generate image of graphics for data from Microsoft SQL Server 2005?

I have the following example table: Dt Value1 Value2 Value3 ... 2008-12-01 12:34:00 100.1 0.123 43 .... Is there any way by using TSQL to generate trend graphics as image such as jpg? Or do I need Reporting service to do it? I need to do it TSQL so that the daily trend images can be generated in a sche...

I'd like to use the "nested set model" but I'm obliged to have a GUID as primary key. How can I do without integers as pk?

I'm not aware how deep my tree will be. So I think the NSM is fit for me, reading some docs. In sql, this model suppose I'm using an integer value as primary key. I thought to create a twin table only to store the ints (PK,left,righ) connected by a relation one-to-one with the real table. Things are complicating and it is a waste of spac...

SQL Logins for the service and cluster accounts

Hi Can anybody please, let me know that why the cluster service needs local SQL logins and the sql server services need SQL logins. Thanks, ...

Getting image height and width when image is saved in database

Hi I save my images into my SQL Server Database with ASP.NET(2.0). (imageData -> image) (imageType -> varchar) (imageLength -> bigint) Thus the imageData will be "Binary data" and the imageType will be like "image/gif" and the imageLength will be like "6458"....... Is it possible to get the image HEIGHT and WIDTH from my VB.NET code ...

How to embed image in html and send html as email by msdb.dbo.sp_send_dbmail?

I can use msdb.dbo.sp_send_dbmail to send out email in html format. It is very nice for text only in terms of format. For example: EXEC msdb.dbo.sp_send_dbmail @recipients = @p_recipients, @subject = @v_subject, @body=@emailHTML, @body_format = 'HTML'; However, if I want to include images such as trend generated from data on ...