Solved

T-SQL syntax question

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

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 3

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now