Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

T-SQL syntax question

Posted on 2013-11-26
5
395 Views
Last Modified: 2013-11-27
Hi all, attached you will see a small peace of code that I am trying to run in sql, can someone give me a hand on how to work this properly?
Thanks.
11-26-13.txt
0
Comment
Question by:bfuchs
  • 2
  • 2
5 Comments
 
LVL 24

Assisted Solution

by:chaau
chaau earned 375 total points
ID: 39679436
You need to escape the single quotes properly. Use two single quotes inside string literals, like this:
declare @p1 varchar(500)
set @p1=' Day >= ''01/01/2006'' AND EmployeeID IN (SELECT EmployeesID FROM EmployeesUnitstbl WHERE Unit = ''abc'') and FacilityID = 1234'
EXEC "dbo"."proc_FilterOrientNotes_Test" @P1 

Open in new window

0
 
LVL 22

Assisted Solution

by:Kelvin Sparks
Kelvin Sparks earned 125 total points
ID: 39679442
Is this sql you have inside of Access, or is this being run in SQL Server? That is, where are you running this from?


Kelvin
0
 
LVL 4

Author Comment

by:bfuchs
ID: 39679488
Hi thanks for replying,
    This is being passed as an stored procedure argument from access to sql, just placed the peace of code from where I was trying to debug it in SSMS.
However, will try modify the code according to what chaau suggested and let you know.
0
 
LVL 24

Accepted Solution

by:
chaau earned 375 total points
ID: 39679517
If it is a passed code you need to use a different approach. Here is a small chunk of VBA code that is calling an SQL Server stored procedure:
Public dbconn As ADODb.Connection
Dim cmd As New ADODb.command
Dim rs As New ADODb.Recordset
Dim param As New ADODb.Parameter

Set dbconn = New ADODb.Connection
dbconn.ConnectionString = GetServerCon

dbconn.Open dbconn.ConnectionString

Set cmd = New ADODb.command
cmd.ActiveConnection = dbconn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "dbo.proc_FilterOrientNotes_Test"

Set param = cmd.CreateParameter(_
   "p1", adVarChar, adParamInput, 500, " Day >= '01/01/2006' AND EmployeeID IN (SELECT EmployeesID FROM EmployeesUnitstbl WHERE Unit = 'abc') and FacilityID = 1234"
cmd.Parameters.Append param

rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open cmd

Open in new window

As you can see, you do not need to escape the single quotes in VB code, as the outer quotes are the double quotes.
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 39682042
Actually from kelvinsparks question & chaau's two answers I figured out where the problem were indeed...
it was something wrong with the sql statement itself (ambiguous field name..).
Thanks very much for your help guys!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question