Way To Escape From CURSOR

May 3, 2002

I am probably a bit of dumb programmer but I am trying to get away from using cursors in my SQL stored procedures. I probably have my VB program code set in my mind but what is the alternative of doing something like this in T-SQL:

do while not recordset.eof
if condition is ok then
update blah blah blah set something = recordset("field")
end if
recordset.movenext
Loop

Can someone give me an example in T-SQl which can achieve the same thing? (Apart from calling SQL stored procedure using ADO within VB?)

Thanks

View 1 Replies


ADVERTISEMENT

ESCAPE Clause

Dec 26, 2007

 SELECT     ID, Firstname, LastnameFROM         Table_1WHERE     (Firstname LIKE '2-%%' ESCAPE '-')
Firstname
2%
267585
why statement above correct work but statement down correct do not work.
 SELECT     ID, Firstname, LastnameFROM         Table_1WHERE     (Firstname LIKE '2-%' ESCAPE '-')
Firstname
null
 
by mohsen

View 2 Replies View Related

ESCAPE Clause

Dec 27, 2007

SELECT ID, Firstname, Lastname FROM Table_1
 WHERE (Firstname LIKE '2-%%' ESCAPE '-')                                                             
ID     FIRSTNAME      LASTNAME
2       2%                  mohsen
 why statement above correct work but statement down correct do not work.                       table
                                                                                                                                    ID          FIRSTNAME    LASTNAME     
SELECT ID, Firstname, Lastname FROM Table_1                                                             1             mohsen         nafisi
   WHERE (Firstname LIKE '2-%' ESCAPE '-')                                                                   2               2%              mohsen
                                                                                                                                     3              25467          89
ID   FIRSTNAME      LASTNAME
NULL    NULL          NULL
 

View 1 Replies View Related

ESCAPE Clause

Dec 28, 2007

explain ESCAPE clause in LIKE clause with example.
no MSDN and Link.
by mohsen

View 2 Replies View Related

ESCAPE Clause

Dec 28, 2007

explain ESCAPE clause in LIKE clause with example.

by mohsen

View 1 Replies View Related

What Is The ESCAPE CHARACTER?

Nov 9, 2004

Hi Ya'll,

I tried searching for "escape character", "quotes", etc, but they didn't work.

I'm having troubling inserting data into my tables, if they have an apostrophe or double quotes. I know that MySQL's escape character is "", but I tried it for MS SQL and it didn't work. HOW DO I INSERT DATA INTO MY DB THAT HAS AN APOSTROPHE OR DOUBLE QUOTE? Thanks.

View 1 Replies View Related

Escape Sequence

Sep 1, 2004

Hi,
I have a question with this query -
SELECT * FROM table1 WHERE column1 = 'T_C_%';

This query returns rows where column1 = "T_Care", "T_CRP" etc etc, whereas I was expecting only rows where column1 = "T_C_Tail", "T_C_Head"

However, when I use an escape character(/), the results are more in the lines of the expected results.

Can somebody explain this?
cheers/- Pradeep

View 2 Replies View Related

Escape Characters

Sep 6, 2005

What should one pass as a field value into a table in the insertstatement if the value contained a percentage symbol (%) or theasterisk symbol (*), since both of these have special wildcardmeanings. What if I want to pass the special meanings and pass them asliterals? Is there any escape character that I must use? I am usingADO.NET v1.1 of the framework with VB.NET. The database is MicrosoftSQL Server 2000.

View 1 Replies View Related

Escape Characters

Sep 26, 2006

Hi,

I need to insert this command into a table, but I can't because.

insert XXX

set column1 = isnull(Title,'') +

case when (case when title is null then 1 else 0 end) = 1 then '' else ' ' end

+ isnull(Last_name,'') +

case when (case when First_name is null then 1 else 0 end) = 1 then '' else ' ' end

+ isnull(First_name,'') +

case when (case when Middle_initial is null then 1 else 0 end)= 1 then '' else ' ' +

isnull(Middle_initial,'') END

I don't know how to use escape sequence, I need insert just once



thanks

View 4 Replies View Related

Escape Characters

Sep 26, 2006

Hi,

I need to insert this command into a table, but I can't because.

insert XXX

set column1 = isnull(Title,'') +

case when (case when title is null then 1 else 0 end) = 1 then '' else ' ' end

+ isnull(Last_name,'') +

case when (case when First_name is null then 1 else 0 end) = 1 then '' else ' ' end

+ isnull(First_name,'') +

case when (case when Middle_initial is null then 1 else 0 end)= 1 then '' else ' ' +

isnull(Middle_initial,'') END

I don't know how to use escape sequence, I need insert just once



thanks

View 4 Replies View Related

Escape Single Qoute '

Jun 25, 2007

Is there a way to escape single quotes ' in a  sql statement that uses a sql data source that is automated? I know you can do it with a string manip and replacing them with double single quotes. I am just looking for a simple way.  Thanks Adam 

View 2 Replies View Related

Escape Sequence In SQL - Urgent

Feb 25, 2002

Hi,

Could any one please tell me what escape sequence can be used to display the data in the next line in sql

Eg

select 'abc ??? def'

Output

abc
def

Thanks
John Jayaseelan

View 3 Replies View Related

Sqlcmd And Escape Characters

Nov 22, 2007

The command below runs fine from within Management Studioexec sp_MSforeachtable @command1="exec sp_spaceused '?' "However, I'd like to run it via sqlcmd. I've tried variouscombinations of escaping the doulbe and single quotes but withoutsuccess.sqlcmd -E -Q "sp_MSforeachtable @command1="exec sp_spaceused '?'""Sqlcmd: 'exec sp_spaceused '?'""': Unexpected argument. Enter '-?'for help.Any ideas?Thanks,M

View 2 Replies View Related

Escape Single Quotes

Dec 4, 2007

Hi,

I want to pass a query to SQL Server 2005 through the table adapter query.

Some of the variables with have single quotes e.g. He's

I know with PHP/MySQL Addslashes() is used.

What is the alternative for C#?

Thanks.

View 2 Replies View Related

How Can I Escape Special Charater....

Aug 29, 2007

here is the query


DECLARE @NENTITY varchar (100); SET @NENTITY = 'HouseDimension';


DECLARE @FLAGVAR nvarchar (10); SET @FLAGVAR = 'Y';



DECLARE @NFLAG NVARCHAR(50);



SET @NFLAG = (SELECT LEFT(@NENTITY, (SELECT CHARINDEX( 'DIM', @NENTITY) -1)) )+ 'LastUpdateFlag';



SET @SQL1 = 'SELECT * FROM ' + @NENTITY + ' WHERE ' + @NENTITY + '.' + @NFLAG + ' = ' + @FLAGVAR


SELECT @SQL1



OUTPUT: SELECT * FROM HouseDimension WHERE HouseDimension.HouseLastUpdateFlag = Y

where as I need an output with Y in single quotes ('Y')
because when I run this query its gives me an error invalid colunm name Y. I tried to use escape sequence but of no use..



Any suggestion please?





View 6 Replies View Related

Export Data To Csv: Escape New Lines?

Aug 3, 2004

Hi,
I need to convert from mssqlto Postgres and I need to export all MS-SQL table data to a CSV or TXT file (one file per table)

Presumably, all data per row (of a table) must be in one line.
Then when you copy to another database, a new line of data means a new row in the table.

However, MS SQL is exporting a large varchar text field as multiple lines. The data itself is many lines, so exporting it causes the data for one row to fall onto many lines.

My question: How do I escape new lines? When MS SQL exports the data, I want to replace all NEW LINES / carriage returns by /n or by <br> tag (since the data will be for web use).

(pls note I am not actually handling the ms sql database, so any response would be greatly appreciated as I advise the person in charge of the mssql db accordingly).

Thanks a lot!

View 1 Replies View Related

Escape Sequences In Stored Procedures

Nov 8, 2007

All,

What is the escape sequence in a stored procedure?

Here is what I'm trying to achieve:

ALTER PROCEDURE Test
(
@Func VarChar(1000)
)

AS
DECLARE @SQL VarChar(8000)
SELECT @SQL = 'SELECT DISTINCT TNAME FROM TABLE WHERE FUNC LIKE ' + @Func

Now, my goal is to add single quote (') before @Func and another one after that. For eg, if @Func is "Test", I want my query to be
SELECT DISTINCT TNAME FROM TABLE WHERE FUNC LIKE 'Test'

and NOT
SELECT DISTINCT TNAME FROM TABLE WHERE FUNC LIKE Test

Any help?

View 8 Replies View Related

/ Forward Slash Escape Character?

Feb 19, 2008

im having trouble getting this to work:

alter proc [ProGeneral_College_Structure] @Year nvarchar(4)
as
begin
DECLARE @SQLStatement nvarchar(1000)

Set @SQLStatement = 'SELECT School AS Level1Code, DIVISIONS.Div AS Level2Code,
DIVISIONS.ProgArea AS Level3Code, DIVISIONS.progName AS LevelName
, ' + SUBSTRING(@Year,1,2) + '/' + SUBSTRING(@Year,3,2) + ' AS AcademicYearID FROM DIVISIONS
WHERE (((DIVISIONS.[' + @Year + '])=1))
ORDER BY DIVISIONS.School, DIVISIONS.Div, DIVISIONS.ProgArea'

EXEC(@SQLStatement)
end

It's something to do with the / concatenation I think, is it an escape character or something tried // obviously and CHAR(47).
before I get comments I know it's dynamic sql and it's not great but I can't edit the divisions table so have to use a dynamic column.

View 11 Replies View Related

How To Make Escape Characters In Varchar

Jan 5, 2007

I am trying to use this:

INSERT INTO BizNames ( [Key], [Name] ) VALUES ( 0, 'Bob's Lumber' );

The apostrophe embedded in the name value is giving me headaches. I tried using double-quotes and [] to delineate the value but then I get complaints that a "Name" is not allowed in this context.

How do you turn the embedded characters into an escape character so they can be ignored by SQL Server and passed into the table field.

View 1 Replies View Related

Escape Character For /SET Option Of Dtexec

Jan 25, 2006

I have a problem setting some variables in a package using the /SET option of dtexec. Specifically when the value I want to set contains a semi-colon. I get an error like:

Argument ""Package.Variables[User::Delim].Properties[Value];^;"" for option "set" is not valid.

I am guessing that I will have to escape the semi-colons somehow, but with what?

Regards,
Lars

View 9 Replies View Related

Transact SQL :: STATIC Defines A Cursor That Makes Temporary Copy Of Data To Be Used By Cursor

Aug 12, 2015

In MSDN file I read about static cursor

STATIC
Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in
tempdb; therefore, modifications made to base tables are not reflected in the data returned by fetches made to this cursor, and this cursor does not allow modifications

It say's that modifications is not allowed in the static cursor. I have a  questions regarding that

Static Cursor
declare ll cursor global static
            for select  name, salary from ag
  open ll
             fetch from ll
 
              while @@FETCH_STATUS=0
               fetch from ll
                update ag set salary=200 where 1=1
 
   close ll
deallocate ll

In "AG" table, "SALARY" was 100 for all the entries. When I run the Cursor, it showed the salary value as "100" correctly.After the cursor was closed, I run the query select * from AG.But the result had updated to salary 200 as given in the cursor. file says  modifications is not allowed in the static cursor.But I am able to update the data using static cursor.

View 3 Replies View Related

Dynamic Cursor Versus Forward Only Cursor Gives Poor Performance

Jul 20, 2005

Hello,I have a test database with table A containing 10,000 rows and a tableB containing 100,000 rows. Rows in B are "children" of rows in A -each row in A has 10 related rows in B (ie. B has a foreign key to A).Using ODBC I am executing the following loop 10,000 times, expressedbelow in pseudo-code:"select * from A order by a_pk option (fast 1)""fetch from A result set""select * from B where where fk_to_a = 'xxx' order by b_pk option(fast 1)""fetch from B result set" repeated 10 timesIn the above psueod-code 'xxx' is the primary key of the current Arow. NOTE: it is not a mistake that we are repeatedly doing the Aquery and retrieving only the first row.When the queries use fast-forward-only cursors this takes about 2.5minutes. When the queries use dynamic cursors this takes about 1 hour.Does anyone know why the dynamic cursor is killing performance?Because of the SQL Server ODBC driver it is not possible to havenested/multiple fast-forward-only cursors, hence I need to exploreother alternatives.I can only assume that a different query plan is getting constructedfor the dynamic cursor case versus the fast forward only cursor, but Ihave no way of finding out what that query plan is.All help appreciated.Kevin

View 1 Replies View Related

Could Not Complete Cursor Operation Because The Set Options Have Changed Since The Cursor Was Declared.

Sep 20, 2007

I'm trying to implement a sp_MSforeachsp howvever when I call sp_MSforeach_worker
I get the following error can you please explain this problem to me so I can over come the issue.


Msg 16958, Level 16, State 3, Procedure sp_MSforeach_worker, Line 31

Could not complete cursor operation because the set options have changed since the cursor was declared.

Msg 16958, Level 16, State 3, Procedure sp_MSforeach_worker, Line 32

Could not complete cursor operation because the set options have changed since the cursor was declared.

Msg 16917, Level 16, State 1, Procedure sp_MSforeach_worker, Line 153

Cursor is not open.

here is the stored procedure:


Alter PROCEDURE [dbo].[sp_MSforeachsp]

@command1 nvarchar(2000)

, @replacechar nchar(1) = N'?'

, @command2 nvarchar(2000) = null

, @command3 nvarchar(2000) = null

, @whereand nvarchar(2000) = null

, @precommand nvarchar(2000) = null

, @postcommand nvarchar(2000) = null

AS

/* This procedure belongs in the "master" database so it is acessible to all databases */

/* This proc returns one or more rows for each stored procedure */

/* @precommand and @postcommand may be used to force a single result set via a temp table. */

declare @retval int

if (@precommand is not null) EXECUTE(@precommand)

/* Create the select */

EXECUTE(N'declare hCForEachTable cursor global for

SELECT QUOTENAME(SPECIFIC_SCHEMA)+''.''+QUOTENAME(ROUTINE_NAME)

FROM INFORMATION_SCHEMA.ROUTINES

WHERE ROUTINE_TYPE = ''PROCEDURE''

AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(SPECIFIC_SCHEMA)+''.''+QUOTENAME(ROUTINE_NAME)), ''IsMSShipped'') = 0 '

+ @whereand)

select @retval = @@error

if (@retval = 0)

EXECUTE @retval = [dbo].sp_MSforeach_worker @command1, @replacechar, @command2, @command3, 0

if (@retval = 0 and @postcommand is not null)

EXECUTE(@postcommand)

RETURN @retval



GO


example useage:


EXEC sp_MSforeachsp @command1="PRINT '?' GRANT EXECUTE ON ? TO [superuser]"

GO

View 7 Replies View Related

SqlConnection Error( Unrecognized Escape Sequence )

Feb 16, 2008

 CSharp:SqlConnection Con = new SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"); its give ErrorUnrecognized escape sequence 

View 12 Replies View Related

How Do I Escape A New Line Character Thats Been Stored In Sql Server

May 30, 2008

Not sure if this is the right forums to ask this question..
I have a website in which i have a text box that has multiline = true... So suppose if if the user enter absbsbcjd and then hits enter and then again enter jfkdjf in my database i been stored as abscbabc twoboxes (new line) and then jfdkf and then sometime later i am taking that plan name and sending it as a subject to an email but since it has a new line character in that field.. i get an error..
 
so how can escape the new line character..
thanks
Karen

View 6 Replies View Related

How To Escape Ampersand While Selecting Or Inserting Data?

Dec 10, 2003

How to escape ampersand while selecting or inserting data?
I know that for single quote we have to use two single quotes.
Thanks
m_nekkanti

View 2 Replies View Related

Quoted Field And Escape Character Issue

Nov 8, 2006

I am attempting to import a flat file and have come accross and issue that I do not know how to fix in SSIS. The issue is that some of the text fields use quoted identifiers. This is not an issue in itself. The problem is they also use quotes as escape character if quotes are on the field.

So I see instances of "" because inside the quoted field is a quote. How do i specify an escape character?

View 1 Replies View Related

SQL 2012 :: Escape For Quote In Install Configuration File

Aug 19, 2014

I've been installing SQL with a unattended install for awhile, and have the following settings in my install_configuration.ini file. I happened to come across a password with a quote in it today, what is the proper way to escape that quote? I've tried the standard backslash method, and found that to not work.

; SQL Server Agent Service Account
AGTSVCACCOUNT="DomainUserName"
AGTSVCPASSWORD="PasswordWith"AQuoteInIt"
; SQL Server Service Account
SQLSVCACCOUNT="DomainUserName"
SQLSVCPASSWORD="PasswordWith"AQuoteInIt"
; SSRS Service Account
RSSVCACCOUNT="DomainUserName"
RSSVCPASSWORD="PasswordWith"AQuoteInIt"

The cool thing is the install apparently tries a few time to authenticate before giving up, because it ends up locking out the account each time.

View 5 Replies View Related

C# UI Escape Character Problem To Execute Command Line

Aug 3, 2006



Hi!

Thanks For your reply!

but this is very urgent please help!!!!!!!

I need one more help in this issue. what if i do not need any "

for e.g: i am trying to set conn string and varaible value

jobCommand = new SqlCommand("xp_cmdshell 'dtexec /f "" + path + "" /Conn "" + Packconn + "" "" + connect + "" '",cconn);

i am getting some value like this :

CommandText "xp_cmdshell 'dtexec /f "D:\SSISProject\Integration Services Project1\ArchiveMainMultiTables.dtsx" /Conn "SE413695\AASQL2005.TestDB;" "Provider=SQLNCLI.1;Data Source=SE413695\AASQL2005;Initial Catalog=TestDB;Provider=SQLNCLI.1;Integrated Security=SSPI;" '" string


I do not need the highlighted escape characters in it. what should i do??????

i need some thing like this :

xp_cmdshell 'dtexec /f "D:SSISProjectIntegration Services Project1ArchiveMainMultiTables.dtsx" /Conn SE413695AASQL2005.TestDB;"Provider=SQLNCLI.1;Data Source=SE413695AASQL2005;Initial Catalog=TestDB;Provider=SQLNCLI.1;Integrated Security=SSPI;"'

Thanks,

Jas

View 2 Replies View Related

Escape Clause Causes Error In Lookup Modified Sql Statement

Jun 8, 2007

I need to use a modified SQL statement for a lookup component. It has an escape clause in it and this causes error:

select * from dbo.typecustomer where ? like '%'+type_subtype +'%'
ESCAPE '_'

Is this is a bug? Any help will be greatly appreciated.

Thanks

Akin

View 1 Replies View Related

T-SQL (SS2K8) :: Find All Instances Of String That Contain Certain Letters - Escape Double Quote

Sep 14, 2015

I am trying to find all instances of a string that contain the letters FSC. While I am able to find most of them, I am unable to find the ones wrapped in double quotes.

Query example:

Select *
from myTable
Where item like '%FSC%'

This works great with the exception of when FSC is surrounded by double quotes.

ex: MySentencecontains"FSC"

I have tried Replace with no luck.

View 3 Replies View Related

Join Cursor With Table Outside Of Cursor

Sep 25, 2007

part 1

Declare @SQLCMD varchar(5000)
DECLARE @DBNAME VARCHAR (5000)

DECLARE DBCur CURSOR FOR
SELECT U_OB_DB FROM [@OB_TB04_COMPDATA]

OPEN DBCur
FETCH NEXT FROM DBCur INTO @DBNAME


WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @SQLCMD = 'SELECT T0.CARDCODE, T0.U_OB_TID AS TRANSID, T0.DOCNUM AS INV_NO, ' +
+ 'T0.DOCDATE AS INV_DATE, T0.DOCTOTAL AS INV_AMT, T0.U_OB_DONO AS DONO ' +
+ 'FROM ' + @DBNAME + '.dbo.OINV T0 WHERE T0.U_OB_TID IS NOT NULL'
EXEC(@SQLCMD)
PRINT @SQLCMD
FETCH NEXT FROM DBCur INTO @DBNAME

END

CLOSE DBCur
DEALLOCATE DBCur


Part 2

SELECT
T4.U_OB_PCOMP AS PARENTCOMP, T0.CARDCODE, T0.CARDNAME, ISNULL(T0.U_OB_TID,'') AS TRANSID, T0.DOCNUM AS SONO, T0.DOCDATE AS SODATE,
SUM(T1.QUANTITY) AS SOQTY, T0.DOCTOTAL - T0.TOTALEXPNS AS SO_AMT, T3.DOCNUM AS DONO, T3.DOCDATE AS DO_DATE,
SUM(T2.QUANTITY) AS DOQTY, T3.DOCTOTAL - T3.TOTALEXPNS AS DO_AMT
INTO #MAIN
FROM
ORDR T0
JOIN RDR1 T1 ON T0.DOCENTRY = T1.DOCENTRY
LEFT JOIN DLN1 T2 ON T1.DOCENTRY = T2.BASEENTRY AND T1.LINENUM = T2.BASELINE AND T2.BASETYPE = T0.OBJTYPE
LEFT JOIN ODLN T3 ON T2.DOCENTRY = T3.DOCENTRY
LEFT JOIN OCRD T4 ON T0.CARDCODE = T4.CARDCODE
WHERE ISNULL(T0.U_OB_TID,0) <> 0
GROUP BY T4.U_OB_PCOMP, T0.CARDCODE,T0.CARDNAME, T0.U_OB_TID, T0.DOCNUM, T0.DOCDATE, T3.DOCNUM, T3.DOCDATE, T0.DOCTOTAL, T3.DOCTOTAL, T3.TOTALEXPNS, T0.TOTALEXPNS


my question is,
how to join the part 1 n part 2?
is there posibility?

View 1 Replies View Related

Cursor Inside A Cursor

Oct 5, 2004

I'm new to cursors, and I'm not sure what's wrong with this code, it run for ever and when I stop it I get cursor open errors




declare Q cursor for
select systudentid from satrans


declare @id int

open Q
fetch next from Q into @id
while @@fetch_status = 0
begin

declare c cursor for

Select
b.ssn,
SaTrans.SyStudentID,
satrans.date,
satrans.type,
SaTrans.SyCampusID,
Amount = Case SaTrans.Type
When 'P' Then SaTrans.Amount * -1
When 'C' Then SaTrans.Amount * -1
Else SaTrans.Amount END

From SaTrans , systudent b where satrans.systudentid = b.systudentid

and satrans.systudentid = @id




declare @arbalance money, @type varchar, @ssn varchar, @amount money, @systudentid int, @transdate datetime, @sycampusid int, @before money

set @arbalance = 0
open c
fetch next from c into @ssn, @systudentid, @transdate, @type, @sycampusid, @amount

while @@fetch_status = 0
begin

set @arbalance = @arbalance + @amount
set @before = @arbalance -@amount

insert c2000_utility1..tempbalhistory1
select @systudentid systudentid, @sycampusid sycampusid, @transdate transdate, @amount amount, @type type, @arbalance Arbalance, @before BeforeBalance
where( convert (int,@amount) <= -50
or @amount * -1 > @before * .02)
and @type = 'P'




fetch next from c into @ssn, @systudentid, @transdate, @type, @sycampusid, @amount
end
close c
deallocate c
fetch next from Q into @id

end
close Q
deallocate Q


select * from c2000_utility1..tempbalhistory1
truncate table c2000_utility1..tempbalhistory1

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved