Trouble with query in vb.net - sql-server

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.

Related

3061 Too few parameters.Expected1

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)

SSRS: is there a way of passing the values of a parameter to a "Values" keyword in the SQL query?

I would like to know the correct method for passing the values of a parameter to a "VALUES" keyword in the SQL in the underlying dataset. I'm using Microsoft Report Builder v3.0, querying an MS-SQL database.
At the moment, after a lot of googling and stack-overflowing, I have come up with the following nicely-working SQL in order to find patients with diagnosis codes starting with either "AB" or "XC":
SELECT
x.PatientId
FROM
(
VALUES
('AB%'),
('XC%')
) AS v (pattern)
CROSS APPLY
(
SELECT
p.PatientId,
p.LastName
FROM
dbo.Patient p
inner join Course c on (c.PatientSer = p.PatientSer)
inner join CourseDiagnosis cd on (cd.CourseSer=c.CourseSer)
inner join Diagnosis diag on (diag.DiagnosisSer=cd.DiagnosisSer)
WHERE
diag.DiagnosisCode like v.pattern
) AS x
;
However, what I want to do is make the patterns searched for, as generated by the "VALUES" keyword, to be generated when the user selects a drop-down box corresponding to a particular group of patterns. I have used a parameter for this named #Diagnoses, with the label "Grouping1" (there will be other groupings later - I intend to make the parameter multi-valued), and the value "'AB%', 'XC%'", but this doesn't work - the report runs, but returns nothing at all, so clearly I'm doing something wrong.
I have tried to avoid specifiying these diagnosis codes directly in the WHERE clause using the "OR" keyword, as everything I can find along these lines seems to involve using separately declared functions, and the pattern specification / cross-applying solution seemed the neatest.
Can someone help me out?
Thanks in Advance.
You can use a JOIN to combine your parameter values and use the Dataset Expression to build the query text.
="SELECT x.PatientId FROM (VALUES ('" & JOIN(Parameters!VALUES.Value, "'),('") & "') ) AS v (pattern) " & VBCRLF &
"CROSS APPLY " & VBCRLF &
<rest of your query>
and the resulting part of the query is:

Cannot input parameters in SQL code for MS query

I'm creating an Excel report, which other users beside myself could manage.
At the moment I'm struggling with creating the parameters in MS Query, so that they would get the input from a cell in Excel.
Everytime I try to paste my code, where I have changed the original dates with the "question marks" I get the following error.
"Parameters are not allowed in queries that can't be displayed grpahically."
I have tried to create parameters for very simple queries. Like
select *
From dmbase.test
where ID = ?
And there the parameters work, I could add even 10 or more and it would still work.
In addition I have tried to paste the code as it is in SSMS and then later through Data-Properties->Query Properties ->Definitions to change the code and add parameters to the "Command Text" box, but Excel just gives an error.
Also tried to declare variables for my parameters, but that didn't work as well. Gave an error regarding symbols.
declare Parameter1 date(10)
set Parameter1 = ?
declare Parameter2 date(10)
set Parameter2 = ?
I'm not 100% sure that the joins are to blame, but that's my feeling.
My code can be seen below.
select
loc.LocationLang
,loc.DistrictLang
,loc.InventoryRegionLang
,cod.ContractNumber
,cus.CustomerLang
,cus.InternalCustomerType
,cus.CustomerGroupCode
,cus.Organizationalcode
,css.CustomerSubSegmentLang
,loc.AnalysisRegionLang
,dht.ContractHeaderTypeLang
,cod.RentalOutDate
from
dmbase.fContractOpenDetail as cod
left join dmbase.dLocation as loc
on cod.LocationID = loc.LocationID
left join dmbase.dCustomer as cus
on cod.CustomerID = cus.CustomerID
left join dmbase.dCustomerSubSegment as css
on cus.CustomerSubsegmentID = css.CustomerSubSegmentID
left join dmbase.dContractHeaderType as dht
on cod.ContractHeaderTypeAMID = dht.ContractHeaderTypeAMID
where
cod.CompanyID = '6'
and cod.RentalOutDate between ? and ?
and left(loc.LocationLang,4) = '7201'
order by cod.ContractNumber
I would appreciate if you could say, how I could put parameters in so that I would not get the error of "Parameters are not allowed...".

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: Updating contents of a field by merging 2 fields together

I wondered if anyone could help me with something I am having some issues with.
TB_SAMPLES contains a field called Notes. This field needs to be updated with the contents of my reference table, SUNCORE_NOTES_UPDATE. I need to add this new data as a prefix to any existing notes.
I have been able to select the values as I would like them to be displayed but I am not able to undertake the update. My 'select' code is below:
select traxx_supportb.[jlr_sql].[suncore_notes_update].notes + ' ' +
tb_samples_nw.notes as fullnotes
from tb_samples_nw, traxx_supportb.[jlr_sql].[suncore_notes_update]
where tb_samples_nw.id = traxx_supportb.[jlr_sql].[suncore_notes_update].id
Can anyone help me with the SQL required to add the contents to TB_SAMPLES.NOTES from SUNCORE_NOTES_UPDATE.NOTES as a prefix?
Thanks very much all!
J
UPDATE t1
SET notes = t2.notes + ' ' + t1.notes
FROM tb_samples_nw t1
JOIN traxx_supportb.[jlr_sql].[suncore_notes_update] t2
ON t1.id = t2.id
Try this:
update tb_samples_nw set notes = IsNull(suncore_notes_update.notes,'')+' '+IsNull(notes,'')
from traxx_supportb.[jlr_sql].[suncore_notes_update]
where tb_samples_nw.id = traxx_supportb.[jlr_sql].[suncore_notes_update].id
You can use an INNER JOIN with an UPDATE. It also makes it cleaner to alias the tables:
UPDATE s
SET s.notes = u.notes
+ ' '
+ s.notes
FROM tb_samples_nw s
INNER JOIN traxx_supportb.[jlr_sql].[suncore_notes_update] U
ON S.id = U.id
Be aware that the original note will be irreversibly modified, meaning that if you run the query again it will prepend the note again.

Resources