Can we add NoLock inside the SQL view - sql-server

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

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.

LEFT join on secure view fails in snowflake

I have a normal view and one secure view and a table.
create or replace NORMALVIEW as select click_id FROM SECURE_VIEW a
LEFT JOIN
TABLE e
ON (e.click_id = a.google_click_id);
select * from NORMALVIEW;
I am getting the error:
SQL execution internal error: Processing aborted due to error 300002:2523989150; incident 8846932.
other operations such as union is working
If you got an incident then you will need to open a case with Snowflake support to understand what is happening.
So I assume the SELECT part is valid:
select e.click_id
FROM SECURE_VIEW a
LEFT JOIN TABLE e
ON (e.click_id = a.google_click_id);
I also assume the view is actually defined more like:
CREATE table test.test.table_name (click_id int);
insert into test.test.table_name values (0),(1);
CREATE SECURE VIEW test.test.SECURE_VIEW AS
SELECT click_id as google_click_id
FROM test.test.table_name
WHERE click_id%2 = 1;
CREATE VIEW test.test.NORMALVIEW AS
SELECT click_id
FROM test.test.SECURE_VIEW AS a
LEFT JOIN test.test.table_name AS e
ON e.click_id = a.google_click_id;
Or some other all valid stuff, thus those views would not be created. for the select to fail on.
I wonder if you grab the DDL of the view and try recreate then (with different names) if they are still valid, or if the shape of some underlying table changed, or if something as be drop/recreate/renamed..
SELECT * from NORMALVIEW;
But yes, at the point open a support ticket.

Querying a server level trigger definition in 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')

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.

Using FOR XML to return child queries

I am doing the subquery below as a parameter in a SELECT statement in SQL Server 2008. Is there any disadvantage of doing this to get a child query of the parent and parse it out on the server when its returned. Seems to easy.
(
SELECT ag.Name, ag.[Description], ega.Gender, ag.[Order], ag.[Type]
FROM GrassrootsHoops.EventAgeGrade ega
INNER JOIN GrassrootsHoops.AgeGrade ag ON ega.AgeGradeId = ag.Id
WHERE ega.EventId = ev.Id FOR XML PATH('AgeGrade'), ROOT('AgeGrades')
) AS AgeGrades

Resources