tsql

Why is variable declared inside IF statement created even when condition evaluates to false?

Since @A is never declared, sql server should throw an error, but it doesn’t. Why is that? DECLARE @i int = 1; IF @i > 10 BEGIN DECLARE @A int = 100; END PRINT @A; // doesn't return any result thanx ...

View changes nvarchars to varchars in SQL Server 2008

I have a view in a SQL Server 2008 db that simply exposes about 20 fields of one table to be consumed via ODBC to a client. When I tried to replicate this view in another database, the client could not consume the data source. Then I noticed some weirdness. The columns in the view are shown, in SQL Server Management Studio, to be varc...

SQL using sp_HelpText to view a stored procedure on a linked server

Anyone have an idea about how to use sp_helptext to view a stored procedure on a linked server? basically something like this. I don't have the credentials to that linked server to look at it. EXEC sp_HelpText '[ServerName].[DatabaseName].dbo.storedProcName' thanks ahead. ...

Is there a way to find TOP X records with grouped data?

I'm working with a Sybase 12.5 server and I have a table defined as such: CREATE TABLE SomeTable( [GroupID] [int] NOT NULL, [DateStamp] [datetime] NOT NULL, [SomeName] varchar(100), PRIMARY KEY CLUSTERED (GroupID,DateStamp) ) I want to be able to list, per [GroupID], only the latest X records by [DateStamp]. The kicke...

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...

Fastest way to do mass update

Let’s say you have a table with about 5 million records and a nvarchar(max) column populated with large text data. You want to set this column to NULL if SomeOtherColumn = 1 in fastest possible way. The brute force UPDATE does not work very well here because it will create large implicit transaction and take forever. Doing updates in sma...

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 ...

SQL Server T-SQL statement to replace/delete sub-strings

Hi, I have a table with 6 columns containing HTML content with some markups in it and now when moving to a new designed site most of this HTML code has to be deleted. More or less all tags except <B> and </B>. Is there a nice way of doing this, identify all tags end delete them within the data? I'm sure there are no < > symbols in the ...

T-SQL - Find differences for PK's in a single table (self join?)

Hi Experts, My situation is this. I have a table of products with a pk "Parent" which has "Components" The data looks something like this Parent(PK) Component Car1 Wheel Car1 Tyre Car1 Roof Car2 Alloy Car2 Tyre Car2 Roof Car3 Alloy Car3 Tyre Car3 Roo...

Synchronizing non-DB SQL Server objects

There are a number of tools available for synchronizing Tables, Indexes, Views, Stored Procedures and objects within a database. (We love RedGate here, and throw a lot of money their way). However, I'm having a very difficult time finding tools that will help with Jobs, Logins and Linked Servers. Do these things exist? Am I missing som...

Can I use TSQL to operate on normal os files? Such as create a .bat file and write some query result into that file?

Hi guys, Can I use TSQL to operate on normal operating system files? Such as create a .bat file at c:\test and write some query result into that batch file? Thanks. ...

Executing a query on csv data stored in an ntext column

Say that the raw text of CSV exports and an associated timestamps are stored in a database, where one record is equivalent to one export. Does anyone have a way to execute a query on the CSV file stored in that field without creating a second connection to the database or exporting the data to a file and then reopening it using the csv ...

SQL To Filter A SubSet Of Data

I have an arraylist that holds a subset of names found in my database. I need to write a query to get a count of the people in the arraylist for certain sections i.e. There is a field "City" in my database from the people in the arraylist of names I want to know how many of them live in Chicago, how many live in New York etc. Can someo...

Optimizing ROW_NUMBER() in SQL Server

We have a number of machines which record data into a database at sporadic intervals. For each record, I'd like to obtain the time period between this recording and the previous recording. I can do this using ROW_NUMBER as follows: WITH TempTable AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY Machine_ID ORDER BY Date_Time) AS Orde...

Selecting a Function Value Shows nvarchar(4000) on an Index

I have a view that I'm trying to setup an Index for. One of the select columns for the view executes a user-defined function that has a return value of varchar(250). However, when I try to setup an Index on that column, I see a size of nvarchar(4000). Why is that and will that cause a problem if I continue to setup my index? ...

Displaying Query Results Horizontally

I am wondering if it is possible to take the results of a query and return them as a CSV string instead of as a column of cells. Basically, we have a table called Customers, and we have a table called CustomerTypeLines, and each Customer can have multiple CustomerTypeLines. When I run a query against it, I run into problems when I want ...

SQL Server 2005, wide indexes, computed columns, and sargable queries

In my database, assume we have a table defined as follows: CREATE TABLE [Chemical]( [ChemicalId] int NOT NULL IDENTITY(1,1) PRIMARY KEY, [Name] nvarchar(max) NOT NULL, [Description] nvarchar(max) NULL ) The value for Name can be very large, so we must use nvarchar(max). Unfortunately, we want to create an index on this col...

SQL Server stored procedure return code oddity

Hello The client that calls this code is restricted and can only deal with return codes from stored procs. So, we modified our usual contract to RETURN -1 on error and default to RETURN 0 if no error If the code hits the inner catch block, then the RETURN code default is -4 rather then 0 Does anyone know where this comes from please?...

Sql combine 2 rows to one

Hi , i have this table Testers employee name ------------ Sam Korch dan mano i want to combine tow rows to one, it will be "Sam Korch,Dan Mano" i have this query select @theString = COALESCE(@theString + ',', '') + EmployeeName from Testers join vw_EKDIR on Testers.TesterGlobalId = vw_EKDIR.GlobalID where TestId = 31 it w...

Column name with special characters

I have web service which returns a datatable with the following column name: Id@qb>type I am applying the following filter statement to this datatable: [Id@qb>type] IN (0, 1, 2, 3, 4) But whenever I use this column name in the filter statement it throws the following exception: Invalid column name 'Id@qb>type'. Thanks. ...