Solved

T-SQL syntax question

Posted on 2013-11-26
5
393 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Viewers will learn how the fundamental information of how to create a table.

810 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