Querying a server level trigger definition in SQL Server - sql-server

I am trying to query the definition text of a server-level trigger. More specifically, trying to query the definition from within a user-defined stored procedure. Because the trigger is saved on the server level, the normal ways do not seem to work.
What I have tried:
1)
sp_helptext: The scope is wrong and I get the message:
Msg 15009, Level 16, State 1, Procedure sp_helptext, Line 54
The object 'myTrigger' does not exist in database 'master' or is invalid for this operation.
2) SELECT OBJECT_DEFINITION (OBJECT_ID(N'myTrigger)) AS ObjectDefinition;:
This returns NULL. I replaced "myTrigger" with the object id from sys.server_triggers. This also returns NULL.
3)
SELECT
name,
s.Definition
FROM
sys.server_triggers t
INNER JOIN sys.sql_modules s
ON t.object_id = s.object_id
WHERE name = 'myTrigger'
The object doesn't exist in sys.sql_modules

Try this
SELECT ssmod.definition AS [Definition]
FROM master.sys.server_triggers AS tr
LEFT OUTER JOIN master.sys.server_assembly_modules AS mod ON mod.object_id = tr.object_id
LEFT OUTER JOIN sys.server_sql_modules AS ssmod ON ssmod.object_id = tr.object_id
WHERE (tr.parent_class = 100) and (tr.name = 'MyTriggerName')

Related

SQL Incorrect syntax: CREATE VIEW must be the only statement in the batch

I'm struggling with a problem in SQL - I keep getting this error:
Incorrect syntax: CREATE VIEW must be the only statement in the batch
I'm using newest Microsoft SQL Server Management Studio.
CREATE VIEW [name]
AS
SELECT
Dostawcy.Nazwa_Firmy,
Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko,
Towary.Nazwa,
SUM(Towary_Dostawy.Ilosc) AS Suma
FROM
Dostawcy
INNER JOIN
Dostawy ON Dostawcy.Nazwa_Firmy = Dostawy.Firma_Dostarczajaca
INNER JOIN
Towary_Dostawy ON Dostawy.ID_Dostawy = Towary_Dostawy.ID_Dostawy
INNER JOIN
Towary ON Towary_Dostawy.ID_Towaru = Towary.ID_Towaru
WHERE
Towary_Dostawy.ID_Towaru = 4
GROUP BY
Towary_Dostawy.ID_Towaru, Towary.Nazwa,
Dostawcy.Nazwa_Firmy, Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko
ORDER BY
Suma DESC
SELECT TOP 1 *
FROM name
I checked if the CREATE VIEW was at the beginning of the code, tried something with GO, but maybe wrong.
Add a go to the line above select top 1 * from [name]
Edit: also, the order by should be removed as it has no effect inside of a view.

Can we add NoLock inside the SQL view

Can we add NoLock inside the SQL view.
For example :- I have one view that name is [dbo].[View_1] - Select * from [dbo].[View_1]
My View script is below.
SELECT dbo.TestOne.ID AS TestOneId, dbo.TestTwo.ID AS TestTwoId, dbo.TestOne.Name AS NameOne, dbo.TestTwo.Name AS NameTwo
FROM dbo.TestOne INNER JOIN
dbo.TestTwo ON dbo.TestOne.ID = dbo.TestTwo.ID
Can I use NoLock inside the view which I mentioned below?
SELECT dbo.TestOne.ID AS TestOneId, dbo.TestTwo.ID AS TestTwoId, dbo.TestOne.Name AS NameOne, dbo.TestTwo.Name AS NameTwo
FROM dbo.TestOne **(Nolock)** INNER JOIN
dbo.TestTwo **(Nolock)** ON dbo.TestOne.ID = dbo.TestTwo.ID

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

Invalid object name in SQL Join

I am switching to SQL Server from the visual editor approach I used in MS Access. Here is my first attempt. I am joining two tables and keep getting an invalid object. Where am I going wrong? The error message specifically says:
(560180 row(s) affected)
Msg 208, Level 16, State 1, Line 20
Invalid object name 'BillingTable'.
My query is:
SELECT [Tracking_Number]
,[Package_Key]
,[Manifest_Datetime]
,[Packed_Datetime]
,[Order_Number]
,[WMS_Order_Number]
,[Shipped_Warehouse_Name]
,[Carrier]
,[Service_Name]
,[Zone]
,[Estimated_Weight]
,[Estimated_Cost]
,[Preferred_Warehouse_Name]
,[WMS_Shipping_Method_Name]
FROM [Shipping].[dim].[tbl_Package] as PackageTable
where [Manifest_Datetime] > '1/1/2016'
SELECT [Invoice_Date_Key]
,[Order_Date_Key]
,[Package_Key]
,[Billed_Weight]
,[Billed_Weight_Metric]
,[Package_Quantity]
,[Total_Cost_Dollars]
,[Tax_Cost_Dollars]
,[Total_Cost_Billed_Currency]
FROM [Shipping].[fact].[tbl_Shipping_Billing] as BillingTable
JOIN BillingTable
on PackageTable.Package_Key = BillingTable.Package_Key
Your query should look like this:
Select PackageTable.*, BillingTable.*
From [Shipping].[dim].[tbl_Package] as PackageTable
Inner Join [Shipping].[fact].[tbl_Shipping_Billing] as BillingTable
on PackageTable.Package_Key = BillingTable.Package_Key
where PackageTable.[Manifest_Datetime] > '1/1/2016'
You can call out the specific fields you want from those tables instead of using the .*

Create Table As With

Im writing sql query and i have issue which i cannot fix.
I'm trying this:
CREATE TABLE a
AS
WITH cteCandidates (Miejscowosc, Ulica, NrDomu, KodPocztowy)
AS
(
SELECT Miejscowosc, Ulica, NrDomu, KodPocztowy
FROM Gimnazja
INTERSECT
SELECT Miejscowosc, Ulica, NrDomu, KodPocztowy
FROM SzkolyPodstawowe
)
Select
e.Lp as 'Gimnazja',
s.Lp as 'Szkoly Podstawowe'
FROM
Gimnazja AS e
INNER JOIN cteCandidates AS c
ON e.Miejscowosc = c.Miejscowosc AND e.Ulica = c.Ulica AND e.NrDomu = c.NrDomu AND e.KodPocztowy = c.KodPocztowy
INNER JOIN SzkolyPodstawowe s
ON s.Miejscowosc = e.Miejscowosc AND s.Ulica = e.Ulica AND s.NrDomu = e.NrDomu AND s.KodPocztowy = e.KodPocztowy
Order By 'Gimnazja'
And errors which i get:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 319, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
And i don't know why. I think that my query is proper but clearly not, and i don't understand why.
Perhaps you want:
WITH cteCandidates (...)
SELECT
e.Lp as 'Gimnazja',
s.Lp as 'Szkoly Podstawowe'
INTO a -- < -- table created here
FROM
Gimnazja AS e
INNER JOIN ...;
CREATE TABLE is used to define a table schema. The WITH keyword is used to define a CTE retrieval of data. These are two completely different operations.
If you need a table within your SQL look at http://msdn.microsoft.com/en-us/library/ms174979.aspx to see the syntax for defining it. You then may populate it with INSERT but that's highly unlikely to be from a CTE ( see below)
Usually with CTEs you don't need to insert the data into a table; in the statement immediately after it you can just use the data retrieved as the table has been temporarily defined and populated for you automatically.
A CTE can be used to define a VIEW - see Create view based on hierarchy / used of CTE for an example
Are you trying to create a view? Then it'd work...

Resources