3061 Too few parameters.Expected1 - sql-server

I am getting the Error two few parameters Expected 1.For the below query which I am using in MS Access vba code .I am able to point that Where clause is the reason I am getting this error.But not sure why.
strSQL ="SELECT Count(*) FROM Table_1 INNER JOIN Table_2 ON (Table_2.[F6] = Table_1.[Column1]) WHERE ( (Table_1.[Table1_Date])> DateAdd(month,-1,Date()) );"
Set rs = MyDB.OpenRecordset(strSQL)

Related

How to mimic the SQL Server VALUES feature in ACCESS?

I have this SQL query in SQL Server
SELECT
v.val
FROM
(VALUES ('MONTROSE'), ('STERLING'), ('GREELEY'), ('FRED'), ('BILL')) v (val)
LEFT JOIN co_wtr t ON t.Quad = v.val
WHERE t.Quad IS NULL;
The point of the query is to find which of he VALUES are not in a table.
Need to run this type of query in ACCESS, but it does not have a VALUES feature. Any suggestions as how to do this in ACCESS.
The gotcha here is that the users don't have write privilege to the database. So the target set of values can't be put into a table.
Not easily, but you can use a UNION ALL query. We use MSysObjects, a default table, here. It can be replaced by any table that has at least one row and has a primary key.
SELECT v.val FROM
(
SELECT TOP 1 'MONTROSE' AS val FROM MSysObjects UNION ALL
SELECT TOP 1 'STERLING' FROM MSysObjects UNION ALL
SELECT TOP 1 'GREELEY' FROM MSysObjects UNION ALL
SELECT TOP 1 'FRED' FROM MSysObjects UNION ALL
SELECT TOP 1 'BILL' FROM MSysObjects
) v LEFT JOIN co_wtr t ON t.Quad = v.val
WHERE t.Quad IS NULL;
Note that we cannot omit the table, Access does do select queries without a FROM (e.g. SELECT 'A' is valid), but not unions of selects without a FROM (SELECT 'A' UNION ALL SELECT 'B' is not valid)
This leads to clunky queries, however. I'd reconsider if a temp table can't be used. Note that the table can be in Access, even though we join it with a linked table on SQL server, so only write permissions on the Access file are needed (and those are always needed to perform locking).
Comprehensive answer from #Erik A, nothing to add.
The method requires to form a string " ('MONTROSE'), ('STERLING'), ('GREELEY'), ('FRED'), ('BILL'))" in VBA and create dynamic query.
In the style inherent in MS Access, I may do the same
Dim OutList As Object
Set OutList = CreateObject("System.Collections.ArrayList")
With OutList
.Add "MONTROSE"
.Add "STERLING"
.Add "GREELEY"
.Add "FRED"
.Add "BILL"
End With
if CheckOutOffTable(OutList) then
...
endif
...
public function CheckOutOffTable( checkList as object) as boolean
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("SELECT t.QUAD FROM co_wtr t GROUP BY t.QUAD")
Do While Not rst.EOF
If checkList.Contains(rst!Quad.Value) Then checkList.Remove rst!Quad.Value
rst.MoveNext
Loop
CheckOutOffTable=(checkList.Count>0)
end function
I like both approaches. Which is preferable in practice?
If this is a application that is linked to SQL server?
Then you could use a pass though query.
eg this:
dim strSQL as string
strSQL = "SELECT v.val FROM (VALUES ('MONTROSE'), ('STERLING'), ('GREELEY'), ('FRED'), ('BILL')) v (val) LEFT JOIN co_wtr t ON t.Quad = v.val"
currentdb.QueryDefs("qryPass").sql = strSQL
' now you can open a report, or form based on above query.

Incorrect syntax near outer join in sql server

I am trying this query to fetch data from two table. I am getting the error on join.
select
bundlechecklist.Bundle_TXN_ID,
documentchecklist.Bundle_TXN_ID,
documentchecklist.Doc_Type_Name
from
bundle_checklist_txn bundlechecklist
outer join
Document_Checklist_TXN documentchecklist on
documentchecklist.Bundle_TXN_ID = bundlechecklist.Bundle_TXN_ID
where
bundlechecklist.originating_tran_id = "AMD1256" and bundle_name = "Line"
Please help me in fixing the error

SQL select works in Management Studio, but not from website

I have the following query:
SELECT
[tblstudentrakazot].studentid, firstname, lastname, tblRakezetSchedule.*
FROM
tblRakezetSchedule
INNER JOIN
[tblstudentrakazot] ON [tblstudentrakazot].scheduleID = tblRakezetSchedule.scheduleid
INNER JOIN
tblstudents ON [tblstudentrakazot].studentid = tblstudents.studentid
WHERE
scheduleday = datepart(w,convert(datetime,'21/2/2016',103))
AND tblRakezetSchedule.rakezetID = 182
ORDER BY
replace(scheduletimefrom, ':', '')
When run from SQL Server Management Studio, it works perfectly and generates 3 records.
When run from my VBscript app, I get no records
The issue seems to be in the filter that refers to the date scheduleday = datepart(w,convert(datetime,'21/2/2016',103)) - when I comment that out, the query works.
Does anyone know what the issue can be?
I have tried playing with the date settings but that didnt change anything.
I have tried doing CONVERT(int,scheduleday) in the WHERE - that didn't help either, as the scheduleday is already a number format field.
I have tried using dw in the datepart instead of w - also no change.
Thanks in advance
UPDATE:
The query works when I do WHERE CONVERT(int,scheduleday) = 1
Also - I see that CONVERT(int,datepart(w,convert(datetime,'21/02/2016',103))) correctly gives 1 too. Its just weird that 1 <> 1 even when they are both converted to INT
Here is my VBscript:
sql = "SELECT [tblstudentrakazot].studentid, firstname, lastname, tblRakezetSchedule.* FROM tblRakezetSchedule"
sql = sql & " INNER join [tblstudentrakazot] on [tblstudentrakazot].scheduleID = tblRakezetSchedule.scheduleid "
sql = sql & " INNER join tblstudents on [tblstudentrakazot].studentid = tblstudents.studentid"
sql = sql & " WHERE "
sql = sql & " CONVERT(int,scheduleday) = datepart(w,convert(datetime,'" & cleanSQL(planneddate) & "',103)) AND "
sql = sql & " tblRakezetSchedule.rakezetID = " & CleanSQL(x_rakezetID)
sql = sql & " ORDER BY replace(scheduletimefrom, ':', '')"
WHERE:
x_rakezetID = 182
and planneddate is a date variable in the format of `21/02/2016'
The result of SELECT datepart(w,convert(datetime,'21/2/2016',103)) will depend on your session's language settings (which will depend on the user.) For example:
SET LANGUAGE German;
SELECT datepart(w,convert(datetime,'21/2/2016',103)) ;
...returns 7, whereas for English it returns 1.
I therefore suspect that you are using different users or at least different language settings when connection through SSMS versus through your application.

SQL Statement with OR never finishes executing, freezes SQL Database

I have an SQL statement
SELECT dbo.sem_computer.COMPUTER_NAME,dbo.sem_computer.COMPUTER_ID,
[IP_ADDR1_TEXT],dbo.sem_computer.COMPUTER_DOMAIN_NAME, [ID],dbo.SEM_AGENT.AGENT_VERSION
FROM sem_computer, [dbo].[V_SEM_COMPUTER], IDENTITY_MAP, SEM_CLIENT,dbo.SEM_AGENT
WHERE [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID and dbo.IDENTITY_MAP.ID = dbo.SEM_CLIENT.GROUP_ID
and dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_CLIENT.COMPUTER_ID
AND dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
AND NAME = 'My Company\Default Group'
OR NAME = 'My Company\Bronx'
order by [IP_ADDR1_TEXT]
That executed indefinitely and even freezes the SQL server when I tried to copy and paste this code.
If I remove the
OR NAME = 'My Company\Bronx'
then the the SQL statement executes just fine
We are using SQL Server 2008
Thank you
I think this whole problem becomes a lot clearer if you write ANSI-Compliant T-SQL. So rather than have your able join conditions in the WHERE clause, you have something like:
SELECT
dbo.sem_computer.COMPUTER_NAME
, dbo.sem_computer.COMPUTER_ID
, [IP_ADDR1_TEXT]
, dbo.sem_computer.COMPUTER_DOMAIN_NAME
, [ID]
, dbo.SEM_AGENT.AGENT_VERSION
FROM
sem_computer AS COM
INNER JOIN
[dbo].[V_SEM_COMPUTER] AS V
ON
COM.COMPUTER_ID = V.COMPUTER_ID
INNER JOIN
dbo.SEM_CLIENT AS CLI
ON
COM.COMPUTER_ID = CLI.COMPUTER_ID
INNER JOIN
dbo.SEM_AGENT AS AGT
ON
COM.COMPUTER_ID = AGT.COMPUTER_ID
INNER JOIN
IDENTITY_MAP AS IM
ON
CLI.GROUP_ID = IM.ID
...
Then your WHERE clause does what it is designed to do, which is filter your data. This becomes, as suggested earlier
WHERE
NAME IN('My Company\Default Group','My Company\Bronx')
The performance problem you had was, as pointed out, you were getting a cross join of all tables. But I think you would have noticed this if you write your joins in an ANSI compliant way.
I hope that helps.
Ash
OR NAME IN('My Company\Default Group','My Company\Bronx')
Should work.
When you have the AND NAME = 'My Company\Default Group'
OR NAME = 'My Company\Bronx' not in parentheses or using an IN clause, it means that it will get everything where the OR condition is satisfied, ignoring all of the other conditions.

Trouble with query in vb.net

I'm getting an error stating that say column doesnt belong to table. Here is my query below, its pretty straight forward.
query = " SELECT A.COLUMN "
" FROM TABLE_1 A, TABLE_2 B"
" WHERE A.COLUMN = B.COLUMN "
For simplicity sake, I put it into a dataset and do a for loop and it works fine. By the way tested query in management studio and it all works fine.
This is where have the problem:
dim new_val as string
new_val = row("A.COLUMN") 'ERROR
NOTE: When i change my query to :
SELECT A.COLUMN **AS COLUMN1**
and then:
new_val = row("COLUMN1") it works.
Does anyone know the reason for this?? Also forgot to note that some columns have the same name thats why i use A.COLUMN.
Just use:
new_val = row("COLUMN")
There is no reason to have the table suffix in your reference, just the column name.
Also, you really should re-write your query to use INNER JOINS:
query = " SELECT A.COLUMN "
" FROM TABLE_1 A INNER JOIN TABLE_2 B ON A.COLUMN = B.COLUMN "
I would suggest that you set the breakpoint in the problematic line and check the name of the column in the quick watch window. In this case, you will be able to determine the column name and thus adjust your code.

Resources