How to pass value from the textbox to the sql query's parameter - sql

I need some help here.
SET #LotId = '{0}%'
SELECT
coalesce(a.LOT_ID, b.LOT_ID,c.LOT_ID,d.LOT_ID) as LotId,
coalesce(a.CheckIn, b.CheckIn,c.CheckIn,d.CheckIn) as CheckIn,
coalesce(a.CheckOut, b.CheckOut,c.CheckOut,d.CheckOut) as CheckOut,
coalesce(a.StatusDesc, b.StatusDesc,c.StatusDesc,d.StatusDesc) as
StatusDesc
FROM
LOT_LOC_BOND a
LEFT OUTER JOIN
LOT_LOC_IEBT b ON a.LOT_ID = b.LOT_ID
AND a.CheckIn = b.CheckIn
AND a.CheckOut = b.CheckOut
AND a.StatusDesc = b.StatusDesc
LEFT OUTER JOIN
LOT_LOC_MBT c ON b.LOT_ID = c.LOT_D
AND b.CheckIn = c.CheckIn
AND b.CheckOut = c.CheckOut
AND b.StatusDesc = c.StatusDesc
LEFT OUTER JOIN
LOT_LOC_SEAL d ON c.LOT_ID = d.LOT_ID
AND c.CheckIn = d.CheckIn
AND c.CheckOut = d.CheckOut
AND c.StatusDesc = d.StatusDesc
WHERE
a.LOT_ID = #LotId
I need to pass a value which known as Lotid from the textbox to the SQL query parameter, and I don't think the above query is a right way to do that.someone told me that using coalesce is forbidden if you want to pass the parameter.
I really need your help right now. I am using SQL Server 2014 for the query and Asp.net, vb.net in Visual Studio 2010 for the IDE, this system also are using two different pages. The textbox is on the first page and the result will be display at the second page.

Related

Converting an update query with inner joins from vba to SQL Server

In converting to VB.Net with SQL Server, I need to convert an update query from vba code to SQL Server format.
VBA:
UPDATE tblEvtStructure
INNER JOIN (tblEvtInput INNER JOIN tblPointsStructure ON
tblEvtInput.EvtPlaced = tblPointsStructure.Position) ON
tblEvtStructure.EvtStruct_Idx = tblEvtInput.EvtStructID
SET tblEvtInput.EvtPoints = [tblPointsStructure].[PointsAwarded]
WHERE tblEvtInput.EvtPlaced = [tblPointsStructure].[Position]
AND tblEvtInput.EvtPlacings = [tblPointsStructure].[InFinal]
AND tblEvtStructure.SectnID < 9 AND tblEvtInput.EvtSingle = 0
My attempt at SQL Server syntax so far - result is "0 rows affected":
UPDATE EI
SET EI.EvtPoints = TPS.PointsAwarded
FROM tblEvtInput AS EI
INNER JOIN tblEvtStructure AS TES ON TES.EvtStruct_Idx = EI.EvtStructID
INNER JOIN tblPointsStructure AS TPS ON TPS.Position = EI.EvtPlaced
AND EI.EvtPlaced = TPS.Position
AND EI.EvtPlacings = TPS.InFinal
WHERE (TES.SectnID < 9) AND (EI.EvtSingle = 0)
I need help trying to get to grips with this please
Your code looks pretty good. The only obvious problem is the qualified column name in set -- not allowed there. So:
UPDATE EI
SET EvtPoints = TPS.PointsAwarded
FROM tblEvtInput EI INNER JOIN
tblEvtStructure TES
ON TES.EvtStruct_Idx = EI.EvtStructID INNER JOIN
tblPointsStructure TPS
ON TPS.Position = EI.EvtPlaced AND
EI.EvtPlaced = TPS.Position AND
EI.EvtPlacings = TPS.InFinal
WHERE (TES.SectnID < 9) AND (EI.EvtSingle = 0);
I removed the ases for the table aliases, as a matter of my personal preference.

Powershell: Get certain attributes from resultset

In a rather small powershell script, I retrieve data from a sql database using invoke-sqlmd. The output is written into a variable. Later in the script, I want to use only one attribute of this result set in a foreach loop. How can I call on that specific attribute?
If the question was not well asked, please feel free to ask for further information.
This is the sqlcmd:
$SQLcmd = "Select * From (SELECT t.[Initiator] ,t.[EndTime] ,t.StartTime ,CONVERT(NVARCHAR(50), t.StartTime, 111) as Datum ,t.[Result] as Result ,GDI.[Name] ,P.DisplayString ,Pi.DisplayString as DisplayStringDeutsch FROM [M42STORE].$SQL_Table t join [dbo].[GDIEImportClassBase] GDI on GDI.ID = t.ImportSequence left join [dbo].[GDIEImportPickupResult] P on P.Value = t.Result left join [dbo].[GDIEImportPickupResult-CI] Pi on Pi.Owner = P.ID and Pi.LCID = 7 ) Q WHERE Result = '3'"

MS access using case in transform pivot

I'm trying to print a schedule using a query in access. The problem is when nobody is scheduled for a task the field is left blank. The problem is when nobody is scheduled for a task for the whole duration of the schedule since nobody is assigned for this task some referencing get borked. So my question is can I use a simple CASE in my query to switch a null value on Responsable.ResponsableAbrege for something like ' ' or 'N/A' or am I looking at it from the wrong angle? Here is the code :
TRANSFORM First(Responsable.ResponsableAbrege) AS PremierDeResponsableAbrege
SELECT wCalendrier.Entite, Entite.DescriptionAbrege, Tache.Tache, Fonction.Abréviation, Module.Module, Fonction.NoFonction, Tache.NoTache, First(wCalendrier.PremierResponable) AS PremierDePremierResponable
FROM RecupererResponsable, Fonction INNER JOIN ([Module] INNER JOIN (Équipe INNER JOIN (((wCalendrier INNER JOIN Tache ON wCalendrier.NoTache = Tache.NoTache) INNER JOIN Responsable ON wCalendrier.NoResponsable = Responsable.NoResponsable) INNER JOIN Entite ON wCalendrier.Entite = Entite.Entite) ON (Équipe.NoÉquipe = Responsable.Equipe) AND (Équipe.NoÉquipe = Responsable.Equipe)) ON Module.NoModule = Tache.Module) ON Fonction.NoFonction = Tache.Fonction
GROUP BY wCalendrier.Entite, Entite.DescriptionAbrege, Tache.Tache, Fonction.Abréviation, Module.Module, Fonction.NoFonction, Tache.NoTache
PIVOT "D" & [Sequence] In ("D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","D14","D15");
In MS Access SQL instead of CASE you should use either IIF or switch
iif(isnull(FIRST(Responsable.ResponsableAbrege)),'N/A', FIRST(Responsable.ResponsableAbrege))

How to create a query using Access from a given SQL

Someone gave me the following SQL and wants me to do a query using Access, so that it returns the information that they need. All dbo_**** are databases where they get the information from.
I tried to do the following SQL using a design view, but its not working, the datasheet(table) doesnt match the information.
I get lost in the lines LEFT OUTER JOIN ... I dont know what to do with anything after that. Can someone help me, please ?
Or if someone someone can edit the SQL so that it works on Access just copying in the SQL view mode.
Thank you.
Declare #data datetime
set #data = '2013-12-31'
--Fundos
SELECT
RelCliSFN.CdCrt,
MC5.Nome 'NomeCarteira',
MC5.CGC 'CNPJCarteira',
player_adm.NomeCurto 'Administrador',
RelCliSFN.Cliente,
CE5.Nome 'NomeCliente',
CE5.CPFCGC 'CPF/CNPJCliente',
VigRelCliSFN.DtIni,
VigRelCliSFN.DtFim
FROM
RelCliSFN
Inner JOIN MC5 ON MC5.Carteira = RelCliSFN.CdCrt
INNER JOIN MC5Auxiliar ON MC5.Carteira = MC5Auxiliar.Carteira
Inner Join VigRelCliSFN ON RelCliSFN.IdRelCliSFN = VigRelCliSFN.IdRelCliSFN
Inner Join CE5 ON CE5.Cliente=RelCliSFN.Cliente
LEFT OUTER JOIN PlayAdmxMC5 ON MC5.Carteira = PlayAdmxMC5.CdCrt AND PlayAdmxMC5.DtIniVig =
(SELECT MAX(DtIniVig) FROM PlayAdmxMC5 WHERE MC5.Carteira = CdCrt AND DtIniVig < MC5.DataAtual)
INNER JOIN Player player_adm ON PlayAdmxMC5.IdPlayAdm = player_adm.ID
WHERE
--MC5Auxiliar.bNaoExpCCSFN is null
((VigRelCliSFN.DtIni <= #data)
and ((VigRelCliSFN.DtFim >= #data) or (VigRelCliSFN.DtFim is null)))
and MC5.CGC <> 0
and CE5.Assessor not in (17, 99)
and CE5.CPFCGC <>0
and MC5.TipoCarteira <> 4
--and RelCliSFN.CdCrt = 3`
As the others already mentioned, your query has MS SQL-Server syntax. So there are three ways you could solve your problem.
Solution 1:
Create a view in the SQL-Server database.
To do this, open your SQL-Server Management Studio. Go to YourServer > databases > YourDatabase > views. Right click on "views" and then select "new view...". Close the "add table" window. Copy your sql-code into the sql window (the one saying "SELECT FROM"). You will get an error message saying that your code can't be displayed. Ignore it. Save your view. You can connect it to your Access database the same way you would connect a table.
Solution 2:
Use a SQL-Passthrough query in Access.
This is a bit complicated to explain, because almost every Access version has a slightly different menu. I'll explain it for Access 2007.
Create a new query. Close the "add table" window. Choose "Pass-Through" from the design menu (I don't know exactly if the menu label of the english version is "design". In my version it says "Entwurf"). Open the properties window. Insert the correct ODBC string. The easiest way to get an ODBC-string is to use one from an existing ODBC-table. Go to the VBA immediate window (Ctrl + G) and write ?CurrentDb.TableDefs("YourTable").Connect (instead of YourTable you have to insert the name of your ODBC-table). Press Return. You should get something like
ODBC;DSN=YourConnection;Description=IAmAnODBCConnection;APP=2007 Microsoft Office system;DATABASE=YourDatabase
Copy this string into the ODBC property of your query. Be sure the option "returns records" is set to "Yes". Copy you SQL into the query and save it.
Solution 3:
Convert your SQL Server syntax to Access syntax. Before doing this, you have to connect every table which is used in the query. I can't test my syntax conversion, but this should work:
PARAMETERS data DateTime = #2013-12-31#;
SELECT
RelCliSFN.CdCrt,
MC5.Nome "NomeCarteira",
MC5.CGC "CNPJCarteira",
player_adm.NomeCurto "Administrador",
RelCliSFN.Cliente,
CE5.Nome "NomeCliente",
CE5.CPFCGC "CPF/CNPJCliente",
VigRelCliSFN.DtIni,
VigRelCliSFN.DtFim
FROM
RelCliSFN
Inner JOIN MC5 ON MC5.Carteira = RelCliSFN.CdCrt
INNER JOIN MC5Auxiliar ON MC5.Carteira = MC5Auxiliar.Carteira
Inner Join VigRelCliSFN ON RelCliSFN.IdRelCliSFN = VigRelCliSFN.IdRelCliSFN
Inner Join CE5 ON CE5.Cliente=RelCliSFN.Cliente
LEFT OUTER JOIN PlayAdmxMC5 ON MC5.Carteira = PlayAdmxMC5.CdCrt AND PlayAdmxMC5.DtIniVig =
(SELECT MAX(DtIniVig) FROM PlayAdmxMC5 WHERE MC5.Carteira = CdCrt AND DtIniVig < MC5.DataAtual)
INNER JOIN Player player_adm ON PlayAdmxMC5.IdPlayAdm = player_adm.ID
WHERE
((VigRelCliSFN.DtIni <= [data])
and ((VigRelCliSFN.DtFim >= [data]) or (VigRelCliSFN.DtFim is null)))
and MC5.CGC <> 0
and CE5.Assessor not in (17, 99)
and CE5.CPFCGC <>0
and MC5.TipoCarteira <> 4
Well I don't know if Access can handle the equation of PlayAdmxMC5.DtIniVig = (Subquery). If it doesn't work, you should create a new query containing only the subquery and then connect it to this query.
MAIN CODE I am using
SELECT dbo_RelCliSFN.CdCrt, dbo_MC5.Nome AS NomeCarteira, dbo_MC5.CGC AS CNPJCarteira, dbo_RelCliSFN.Cliente, dbo_CE5.Nome AS NomeCliente, dbo_CE5.CPFCGC AS [CPF/CNPJCliente], dbo_VigRelCliSFN.DtIni, dbo_VigRelCliSFN.DtFim
FROM ((((dbo_RelCliSFN INNER JOIN dbo_CE5 ON dbo_RelCliSFN.Cliente = dbo_CE5.Cliente) INNER JOIN dbo_MC5 ON dbo_RelCliSFN.CdCrt = dbo_MC5.Carteira) INNER JOIN dbo_MC5Auxiliar ON dbo_MC5.Carteira = dbo_MC5Auxiliar.Carteira) INNER JOIN dbo_VigRelCliSFN ON dbo_RelCliSFN.IdRelCliSFN = dbo_VigRelCliSFN.IdRelCliSFN) LEFT JOIN Query1 ON dbo_MC5.Carteira = Query1.CdCrt
GROUP BY dbo_RelCliSFN.CdCrt, dbo_MC5.Nome, dbo_MC5.CGC, dbo_RelCliSFN.Cliente, dbo_CE5.Nome, dbo_CE5.CPFCGC, dbo_VigRelCliSFN.DtIni, dbo_VigRelCliSFN.DtFim, dbo_MC5.TipoCarteira, dbo_CE5.Assessor
HAVING (((dbo_RelCliSFN.CdCrt)=802) AND ((dbo_MC5.CGC)<>0) AND ((dbo_CE5.CPFCGC)<>0) AND ((dbo_VigRelCliSFN.DtIni)<=#12/31/2013#) AND ((dbo_VigRelCliSFN.DtFim)>=#12/31/2013# Or (dbo_VigRelCliSFN.DtFim) Is Null) AND ((dbo_MC5.TipoCarteira)<>4) AND ((dbo_CE5.Assessor) Not In (17.19)));

VB.NET Reportviewer How to make Inner Join work properly

I have 2 tables
tableAssembly, with columns SerialNumber, Brick1SerialNumber, Brick2SerialNumber,DateInserted
tableBricks, with columns SerialNumber, Lot, Weight, DateMeasured
In VB.NET(WinForms) I have been able to get the reportviewer control to work and print out information from both tables and also to enable/disable columns, and apply filters such as LIKE
In case it isn't clear tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber
What I now want to do is when a user prints out a report from tableAssembly, I want them to be able to filter based on Brick1SerialNumber.Lot or Brick2SerialNumber.Lot or Brick1SerialNumber.DateMeasured or Brick2SerialNumber.DateMeasured
I understand I need to INNER JOIN tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber AND tableAssembly.Brick2SerialNumber = tableBricks.SerialNumber
Do I also need to INNER JOIN the other columns from tableBricks to columns in tableAssembly? or does the INNER JOIN of Brick1SerialNumber = SerialNumber and Brick2SerialNumber = Serial make it so I can filter based on .Lot?
There are two ways to approach this. 1) join to tableBricks twice (probably not necessary for this condition) or attach it once, using both foreign keys (works for this condition, but not for all conditions).
Try this query:
SELECT tableAssembly.*
FROM tableAssembly INNER JOIN tableBricks
ON tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber
OR tableAssembly.Brick2SerialNumber = tableBricks.SerialNumber
WHERE tableBricks.Lot = 99 --actually means Brick1.Lot or Brick2.Lot
AND tableBricks.DateMeasured = '1/1/2000'
If you need a specific Lot or DateMeasured for Brick1 and Brick2 (not the same values), then try this query:
SELECT tableAssembly.*
FROM tableAssembly INNER JOIN tableBricks AS tableBricks1
ON tableAssembly.Brick1SerialNumber = tableBricks1.SerialNumber
INNER JOIN tableBricks AS tableBricks2
ON tableAssembly.Brick2SerialNumber = tableBricks2.SerialNumber
WHERE tableBricks1.Lot = 98
AND tableBricks2.Lot = 99
AND tableBricks1.DateMeasured = '1/1/2000'
AND tableBricks2.DateMeasured = '1/2/2000'