Getting error while using sp_helptext on view - sql-server

I am trying to read the code of a 'view' previously created by someone else ,
when i execute this line of code
EXEC sp_helptext [view name]
i get the following error
Msg 15197, Level 16, State 1, Procedure sp_helptext, Line 110
There is no text for object '_RGEN_ITEMVENEX'.
any idea of how to solve this error ?

you should try to grant VIEW DEFINITION to the desired schema's contained in the database to this role.
Refer to this : https://learn.microsoft.com/fr-fr/sql/t-sql/statements/grant-database-permissions-transact-sql?view=sql-server-2017

Related

Calling stored procedure from code causes error

I am trying to call a stored procedure from code using .Net Core 3.0 and SQL Server 2012, but I keep getting this error:
SqlException: Incorrect syntax near the keyword 'exec'. Incorrect syntax near ')'.
My code:
var policycontacts = await _dbContext.PolicyContacts
.FromSqlInterpolated($"exec dbo.spapi_contact_getbypolicy {input}")
.FirstOrDefaultAsync()
.ConfigureAwait(false);
SQL code:
EXEC Sp_executesql
N'SELECT TOP(1) [p].[ID], [p].[ActionsToTake],
[p].[AddedBy], [p].[BirthCountry], [p].[ContactGUID],
[p].[ContactID], [p].[ContactStatus], [p].[DOBFormation],
[p].[Domicile], [p].[EnhancedDueDiligence], [p].[EntityName],
[p].[EstimatedAmountAssets], [p].[FirstName], [p].[Gender],
[p].[HowClientMet], [p].[LastModifiedBy], [p].[LastModifiedDate],
[p].[LastName], [p].[LastOpenDate], [p].[LastOpenedBy],
[p].[MiddleName], [p].[Notes], [p].[OccupationBusiness],
[p].[OnlineUser], [p].[OpeningNotes], [p].[OriginAssets],
[p].[PEP], [p].[PEPDescription], [p].[PersonalSituation],
[p].[RiskOverride], [p].[Sysdate] FROM (exec dbo.spapi_contact_getbypolicy #p0)AS [p]',
N'#p0 nvarchar(4000)',
#p0=N''
Complete error:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'exec'
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'
Not enough rep to leave a comment, so I have to leave you an answer. Sorry, I think this is more of a comment.
I have a "okay" recommendation, but it might not be the best answer. Depending on how complex your stored procedure is and if you really want to call it in the FROM section, then change your stored procedure into a table valued function. The reason why its a terrible recommendation is there are limitations and performance issues by using a function. I'd read up on the following articles and see which route you would like to take.
Table Valued Function Killing My Query Performance
https://blogs.msdn.microsoft.com/psssql/2010/10/28/query-performance-and-multi-statement-table-valued-functions/
Cross Apply with table valued function article:
https://www.sqlshack.com/the-difference-between-cross-apply-and-outer-apply-in-sql-server/
The other option could be the one where Sean Lange provided.

How to create schema binding and index on view from other server?

I have tried creating a view with the help of schema binding and indexing which is referring from other server table. But sql thrown some error for the below query .
create VIEW [dbo].[Vxyz]
with schemabinding
AS
SELECT
ELID,USECOUNT,LASTUPDATE,TYPE,CODENE,CASNUE,NAME_ENG,ISGROUP,CHGROUP,DLink
IDE,LOCKBY,PhyApB,BUILDNO,PMNNumE,EINECE
FROM IADL.dbo.tblxyz
GO
create unique clustered index IDX_xyz on [dbo].
[Vxyz](ELID)
Found below error
Msg 4512, Level 16, State 3, Procedure IADL.dbo.tblxyz, Line 3 [Batch Start Line 11]
Cannot schema bind view '[dbo].[Vxyz]' because name 'IADL.dbo.tblxyz' is invalid for schema binding. Names must be in two-part format
and an object cannot reference itself.
Msg 1939, Level 16, State 1, Line 17
Cannot create index on view '[dbo].[Vxyz]' because the view is not schema bound.
select distinct
ISNULL(A.elid, B.elid) ElementID,
CASE when A.elid is null and B.elid is not null then 'Missing ElementID :'+
B.elid+' in Mainproductsall table' when A.elid is not null
and B.elid is null then 'Missing ElementID :'+ A.elid+' in Genproductsall table' Else 'OK'
end Datastatus
into ABC
from [dbo].[Vxyz] As A
full outer join [dbo].[Vxyzwa] as B on A.elid = B.elid
where A.elid is null or B.elid is null
Each from from above query is view . As per my first query above which is referring from other server. so i want to optimize and i am trying to create index.
If you check official documentation, you will see that it is stated as follows
All referenced objects must be in the same database.
So you cannot refer a base table from an other database.
This means, for all referenced current database objects should be referenced with their schema name and object name.

CASE STATEMENT for create view in SQL Server 2008

I have a create view query and I want to check if it does not exist yet, then create view. I tried to create like this:
CASE WHEN IS NOT EXISTS vw_Delays
THEN
VIEW vw_Delays AS
SELECT RD_RfileID_fk_ind, SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays FROM dbo.t_RfDelay
GROUP BY RD_RfileID_fk_ind
END
but it returns these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'END'.
How to solve this? Please can anyone help me to fix this ?
You need to use this code to check for the view's existence:
IF NOT EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
.....
The next obstacle you'll encounter is the fact that the CREATE VIEW statement must be the first of a SQL batch - so you cannot have it right after the existence check.
What I usually do is the opposite:
check if the view does exist
and if so - drop the existing view
then create the view from scratch
and I use this code for this setup:
IF EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
DROP VIEW dbo.vw_Delays;
GO
CREATE VIEW dbo.vw_Delays
AS
SELECT
RD_RfileID_fk_ind,
SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays
FROM
dbo.t_RfDelay
GROUP BY
RD_RfileID_fk_ind

Checking if oracle table exists and creating it afterwards - ORA-06550

I'm trying to check if table exists and if not, then I want to create it. But I'm still getting this error:
Error report -
ORA-06550: řádka 30, sloupec 28:
PL/SQL: ORA-00942: tabulka nebo pohled neexistuje
ORA-06550: řádka 30, sloupec 3:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
My code is:
SELECT COUNT(*) into cnt FROM dba_tables WHERE owner = 'CENTRUMADMIN' and table_name = 'AUD$_BACKUP';
DBMS_OUTPUT.PUT_LINE(cnt);
IF (cnt <= 0) THEN
EXECUTE IMMEDIATE 'CREATE TABLE CENTRUMADMIN.AUD$_BACKUP AS select * from SYS.AUD$ where 1=2';
DBMS_OUTPUT.PUT_LINE('Vytvorena tabulka AUD$_BACKUP ve shematu CENTRUMADMIN:');
END IF;
This code is inside my procedure for cleaning audit trail.
Can anyone pls help? Thanks in advance!
As I read in your comments you said you get the error only when the table AUD$_BACKUP does not exist.
which means you get you error here:
EXECUTE IMMEDIATE 'CREATE TABLE CENTRUMADMIN.AUD$_BACKUP AS select * from SYS.AUD$ where 1=2';
I guess you just dont have permissions for sys.AUD$ in you schema.
please notice that PL/SQL code can use role privileges. which means, even if you have "dba" role on your schema, it wont work. so you should run the following grant command:
Alter user myuser select on sys.AUD$
good luck.

SQL Server error

I am getting a fatal error on following code.
exec [sp_ExternalApp_UPDATEUSER] 'ZZZ', XXXXX','DDDDD','DDDFFDD','EREE', 'EREWWWWW',1,1,'QWEW#DFEE.DER','DEFF','XXXX','DDDD'
Following error occurred:
Location: memilb.cpp:1624
Expression: pilb->m_cRef == 0
SPID: 79
Process ID: 2256
Msg 3624, Level 20, State 1, Procedure sp_ExternalApp_UPDATEUSER, Line 32
A system assertion check has failed. Check the SQL Server error log for details
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
Thank you
I got the solution.
I have used UPPER() function. As I removed that function, my problem solved
Like the message said, check your error log, there might be more detail in there..what does this proc do does it use sp_OACreate or calls xp_cmdshell? Post the proc code
You might want to check the database for corruption. Try
DBCC CHECKDB
(before doing so, read the documentation on that command! See http://msdn.microsoft.com/en-us/library/ms176064.aspx )

Resources