Can someone explain this code line by line? I would like to understand the purpose and effect of each line this code has. This code is from Microsoft and doesn't have the best description of what is going on. I plan to use this code and would like to know where the generic names are as well vs the syntactically correct code. THANKS!!
From Microsoft, looked at their documentation but can't find everything
enter code here
USE DATABASE;
GO
SELECT *
FROM fn_trace_gettable
(drive, default);
GO
SELECT
TE.name AS [DLL_EDIT],
TT.DatabaseName,
TT.DatabaseID,
TT.ApplicationName,
TT.LoginName,
TT.Duration,
TT.Duration,
TT.StartTime,
TT.EndTime
FROM sys.fn_trace_gettable(
CONVERT (VARCHAR(4000),
(
SELECT TOP 1
f.[value]
FROM sys.fn_trace_gettable(NULL) f
WHERE f.property = 2
)), DEFAULT
) TT
JOIN
sys.trace_events TE
ON TT.EventsClass = TE.trace_events_id
ORDER BY TT.StartTime desc;
))
)
Related
I am working on a web app and there are some long winded stored procedures and just trying to figure something out, I have extracted this part of the stored proc, but cant get it to work. The guy who did this is creating alias after alias.. and I just want to get a section to work it out. Its complaining about the ending but all the curly brackets seem to match. Thanks in advance..
FInputs is another stored procedure.. the whole thing is referred to as BASE.. the result of this was being put in a temp table where its all referred to as U. I am trying to break it down into separate sections.
;WITH Base AS
(
SELECT
*
FROM F_Inputs(1,1,100021)
),
U AS
(
SELECT
ISNULL(q.CoverPK,r.CoverPK) AS CoverPK,
OneLine,
InputPK,
ISNULL(q.InputName,r.InputName) AS InputName,
InputOrdinal,
InputType,
ParentPK,
InputTriggerFK,
ISNULL(q.InputString,r.InputString) AS InputString,
PageNo,
r.RatePK,
RateName,
Rate,
Threshold,
ISNULL(q.Excess,r.Excess) AS Excess,
RateLabel,
RateTip,
Refer,
DivBy,
RateOrdinal,
RateBW,
ngRequired,
ISNULL(q.RateValue,r.RateValue) AS RateValue,
ngClass,
ngPattern,
UnitType,
TableChildren,
TableFirstColumn,
parentRatePK,
listRatePK,
NewParentBW,
NewChildBW,
ISNULL(q.SumInsured,0) AS SumInsured,
ISNULL(q.NoItems,0) AS NoItems,
DisplayBW,
ReturnBW,
StringBW,
r.lblSumInsured,
lblNumber,
SubRateHeading,
TrigSubHeadings,
ISNULL(q.RateTypeFK,r.RateTypeFK) AS RateTypeFK,
0 AS ListNo,
0 AS ListOrdinal,
InputSelectedPK,
InputVis,
CASE
WHEN ISNULL(NewChildBW,0) = 0
THEN 1
WHEN q.RatePK is NOT null
THEN 1
ELSE RateVis
END AS RateVis,
RateStatus,
DiscountFirstRate,
DiscountSubsequentRate,
CoverCalcFK,
TradeFilter,
ngDisabled,
RateGroup,
SectionNo
FROM BASE R
LEFT JOIN QuoteInputs Q
ON q.RatePK = r.RatePK
AND q.ListNo = 0
AND q.QuoteId = 100021 )
Well, I explained the issue in the comments section already. I'm doing it here again, so future readers find the answer more easily.
A WITH clause is part of a query. It creates a view on-the-fly, e.g.:
with toys as (select * from products where type = 'toys') select * from toys;
Without the query at the end, the statement is invalid (and would not make much sense anyhow; if one wanted a permanent view for later use, one would use CREATE VIEW instead).
I am tried to format SQL file with IntelliJ IDEA and got something like this:
INSERT IGNORE INTO SpectralImages
(scanDataID,
imageToolsetID,
imageTypeID,
imageFilename,
imageDSPath,
fileID)
(SELECT
ScanData.scanDataID,
1,
Files.imageTypeID,
Files.fileName,
Files.fileDSPath,
Files.fileID
FROM ((
Files
LEFT JOIN
Samples_Files
ON (
Samples_Files.fileID
=
Files.fileID)) LEFT JOIN
Scans
ON (
Scans.sampleID
=
Samples_Files.sampleID)) JOIN
ScanData
ON (
ScanData.scanID
=
Scans.scanID)
WHERE
Files.imageTypeID
IS
NOT
NULL
AND
Files.fileID
NOT
IN
(SELECT fileID
FROM
SpectralImages)
GROUP BY
scanDataID,
Files.fileID);
Apparently it tries to put each term into separate line. Is this normal?
Are there any other formatting options with more reasonable line wrapping?
IntelliJ IDEA offers a lot of formatting options for SQL, you can disable newlines where needed:
This view is what I am working on. It works fine,
but I intend to add the COALESCE statement in the commented
out portion to this Script. Would anyone know how to write the
Script properly. When I combined them, there was an error.
CREATE VIEW [dbo].[VW_Bzo_D]
AS WITH today AS
(SELECT *
FROM [dbo].[Bz_DAYS]
WHERE [DATE] = CAST(GETDATE() AS DATE)
),
pd AS (SELECT [DATE] AS REPORTING_PERIOD
FROM dbo.Bz_DAYS
WHERE DAY([DATE]) = 1
)
SELECT sp.*,
rp.REPORTING_PERIOD,
ac.DATE_ORDINAL AS CUSTOMER_ACCEPTANCE_ORDINAL,
mv.DATE_ORDINAL AS CUSTOMER_MOVE_ORDINAL,
today.DATE_ORDINAL TODAY_ORDINAL
/*sp.[CUSTOMER_MOVE], sp.[CUSTOMER_REQUESTED], sp.[LEASE_SIGNED_BY_GSA],
sp.[SUBMITTED_TO_GSA], sp.[CUSTOMER_ACCEPTANCE],
COALESCE(sp.[CUSTOMER_MOVE], sp.[CUSTOMER_REQUESTED], sp.[LEASE_SIGNED_BY_GSA],
sp.[SUBMITTED_TO_GSA], sp.[CUSTOMER_ACCEPTANCE]
) AS REPORT_MONTH
*/
FROM dbo.Bzo_Den sp
The error is :
Column names in each view or function must be unique. Column name
'CUSTOMER_MOVE' in view or function 'VW_Bzo_D' is specified more than
once.
I dont know if this will help but did you try aliasing the column names which is giving you trouble?
One of the other users had a similar problem and he tried aliasing. Please look at the link below
Create view in sql server "names in each view or function must be unique"
Hope it helps.
I created an SCCM SQL report using SQL Management Studio. I then created the following prompts for my Asset Management Office to use on the web report: Publisher, Display Name, and Version.
The Display Name and the Version prompts are both optional.
I receive no syntax errors or anything, but I receive absolutely no results whatsoever when I click on the Display button to produce the web report.
Here is my SQL code:
==================================================================================
SELECT dbo.v_R_System.Netbios_Name0, dbo.v_GS_ADD_REMOVE_PROGRAMS.DisplayName0, dbo.v_GS_ADD_REMOVE_PROGRAMS.Version0,
dbo.v_GS_ADD_REMOVE_PROGRAMS.Publisher0
FROM dbo.v_R_System INNER JOIN
dbo.v_GS_ADD_REMOVE_PROGRAMS ON dbo.v_R_System.ResourceID = dbo.v_GS_ADD_REMOVE_PROGRAMS.ResourceID
WHERE dbo.v_GS_ADD_REMOVE_PROGRAMS.DisplayName0 = #DisplayName AND dbo.v_GS_ADD_REMOVE_PROGRAMS.Version0 = #Version AND dbo.v_GS_ADD_REMOVE_PROGRAMS.Publisher0 = #Publisher
Order by dbo.v_GS_ADD_REMOVE_PROGRAMS.DisplayName0 ASC
==================================================================================
I run my report and in the Publisher prompt, I type in something like %Autodesk% and then I click the Display button, and absolutely nothing is displayed. I can go to another report and look up Autodesk products, but not this one. I am not a well versed SQL guy, so if anyone can help me that would be great.
Thanks
% signs often mean wildcards. They are used with the like keyword. You have equal signs. In other words,
where DisplayName like '%fred%'
will return fred, freddie, frederick, etc. However,
where DisplayName = '%fred%'
will only return %fred%
AHHHH I see what you were talking about Dan.
Disregard my last statement/inquiry everyone, I got my code working now. My Prompt #variables' SQL code was not configured properly to work with the "Like" constant so I modified my main SQL code to be this:
==================================================================================
Select Distinct dbo.v_R_System.Netbios_Name0, dbo.v_GS_ADD_REMOVE_PROGRAMS.DisplayName0, dbo.v_GS_ADD_REMOVE_PROGRAMS.Version0, dbo.v_GS_ADD_REMOVE_PROGRAMS.Publisher0
FROM dbo.v_R_System
Join dbo.v_GS_ADD_REMOVE_PROGRAMS On dbo.v_R_System.ResourceID = dbo.v_GS_ADD_REMOVE_PROGRAMS.ResourceID
Where dbo.v_GS_ADD_REMOVE_PROGRAMS.Publisher0 Like #Publisher Or dbo.v_GS_ADD_REMOVE_PROGRAMS.DisplayName0 Like #DisplayName Or dbo.v_GS_ADD_REMOVE_PROGRAMS.Version0 Like #VersionOrder by dbo.v_GS_ADD_REMOVE_PROGRAMS.DisplayName0 ASC
==================================================================================
Then I had to modify my Prompt SQL code to use the Begin, If, Else, and End statements. I will pick just my #Publisher variable Prompt SQL code since all of my Prompt #variables' SQL code are basically identical.
Here is the SQL code for my Publisher (#Publisher) Prompt:
==================================================================================
Begin
If(#__filterwildcard = '')
Select Distinct Publisher0
From v_Add_Remove_Programs Order By Publisher0
Else
Select Distinct Publisher0
From v_Add_Remove_Programs
Where Publisher0 Like #__filterwildcard
Order By Publisher0
End
==================================================================================
The code I used before for my Prompt #variables was like this which did not work with the "Like" constant in my main SQL code:
==================================================================================
Select Distinct Publisher0
From v_Add_Remove_Programs Order By Publisher0
Where Publisher0 Like #__filterwildcard
Order By Publisher0
==================================================================================
So, apparently using the Begin, If, Else, and End statements allowed for more logic in this query process, therefore allowing me to use the "Like" constant in my main SQL code for the report.
SQL query in Ms-Access
INSERT INTO tblTmpEventLog( TrackingNumber, PartNumber, PartNumberChgLvl,
EnteredBy, EventTypeSelected, EventDate )
SELECT DISTINCT tblRevRelLog_Detail.RevRelTrackingNumber,
tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel,
[Forms]![frmEventLog_Input]![EnteredBy] AS EnteredBy,
[Forms]![frmEventLog_Input]![EventTypeSelected] AS EventTypeSelected,
CDate([Forms]![frmEventLog_Input]![EventDate]) AS EventDate
FROM tblRevRelLog_Detail LEFT JOIN tblEventLog
ON (tblEventLog.PartNumber = tblRevRelLog_Detail.PartNumber)
AND (tblEventLog.PartNumberChgLvl = tblRevRelLog_Detail.ChangeLevel)
WHERE ((([tblRevRelLog_Detail]![RevRelTrackingNumber]) =
[Forms]![frmEventLog_Input]![TrackingNumber]))
AND ((tblEventLog.PartNumber) NOT IN
(SELECT tblEventLog.PartNumber FROM tblEventLog
WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper'
AND tblEventLog.TrackingNumber =
tblRevRelLog_Detail.RevRelTrackingNumber
AND tblEventLog.PartNumber = tblRevRelLog_Detail.PartNumber
AND tblEventLog.PartNumberChgLvl =
tblRevRelLog_Detail.ChangeLevel
));
DISTINCT keyword for EnteredBy, EventTypeSelected is not working..I mean, data for these columns is not displaying when I use DISTINCT keyword.
EVENTDATE is working fine, but I do not understand why is it not displaying for EneteredBy and EventTypeSelected columns.
Can anyone tell me how to handle this?
It may be that the query can't interpret properly from the form directly as the final data type. However in your date field, you are wrapping it in a function CDATE( ... ). So, the SQL engine knows the result type. I would suggest doing the same for the other fields. Ex: doing a CAST ( ...your form control... as DateTime ) as OtherColumn, etc... I THINK Access allows casting, but not positive. Otherwise, pre-pull the form value into a declared data type variable and use THAT variable in the query AS OtherColumn as you are doing.
Additionally to what #Jack mentioned, you can always go back to your account, look at your historical question, and click on whatever answers actually helped / solve your problems. Some questions never do get answers and that's ok, just give credit to those that DO help.
I have found in the past (I don't remember which old version of Access this was) that if you set the value of a form control in VBA, and then use that control in a query, the query will not see the value you set in VBA. If the user edits the control normally, the query sees the expected value. Perhaps that's what happened here.
To work around that, you can declare a VBA function that returns the desired value. For example, instead of this:
SELECT ..., Forms!MainForm!TextEntry AS TextEntry, ... FROM ...
use this:
SELECT ..., GetTextEntry() AS TextEntry, ... FROM ...
along with this:
Public Function TextEntry() As Variant
TextEntry = Forms!MainForm!TextEntry
End Function