Solved

T-SQL syntax question

Posted on 2013-11-26
5
383 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

763 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

8 Experts available now in Live!

Get 1:1 Help Now