What could cause SSMS to not parse SQL properly? [closed] - ssms

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
This is the darndest thing I've ever seen.
I created a proc from a template that uses SYSNAME as the parameter types. All portions of the proc that took the name from the parameter are throwing errors. Here is a sample:
IF EXISTS(select 1 from sysobjects where name=N'dbo.ms_lst_partner_break_types' and xtype='p')
BEGIN
PRINT 'DROP PROCEDURE dbo.ms_lst_partner_break_types'
DROP PROCEDURE dbo.ms_lst_partner_break_types
END
Here is the error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '_partner_break_types'.
The weirdest thing is that when I double click on dbo.ms_lst_partner_break_types SSMS highlights either ms_lst or _partner_break_types depending on where I click. Copy the script into Textpad and back, same problem. Remove _partner_break_types and suddenly it works.
Does anyone have any idea what gives?

I don't know why it happened, but Unicode character 0x1f was inserted into the script for some reason. It might be a bug in SSMS, but I don't think it's going to be answered that easily.

In SQL Server 2008, it's sys.objects. Also, the field to look for the "name" is different, as well as other general syntax:
IF EXISTS (SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[my_proc]')
AND type in (N'P', N'PC'))
The easiest thing to do is to right-click on the SP and select "script as drop to new query window" via the context menu heirarchy.

Related

Hello this is my first time creating a procedure but I keep getting the SQL STATEMENT IGNORED error. Any help would be appreciated

My Procedure
The errors I am getting.
Check your schema.table_name. On your insert statement, you have MEMBER_ONLY yet on your update, you have MEMBERS_ONLY (Plural).
Also, if I am not mistaken the top part is also incorrect.
CREATE PROCEDURE procedurename
(
#paramname int
)
As
This is, obviously, Oracle.
Error stack points to exact error place, e.g.
Error(22,29): PL/SQL: ORA-00942: table or view does not exist
-- --
^ column 29
|
line 22
It would be easier to spot it if you chose to display line numbers in SQL Developer (do so; right-click the left margin and set it). I'd say that it is about member_system.member table. Another one, in the same from clause, is prospect_staging.magi_applicant.
It is unclear which user you're connected to (one, or none of these), but - comment you posted in deleted answer:
It works in the workbench but when I place it in the procedure it says the table or view isn't created
suggests that you might have got access to the table via role (and not directly to your user). Why? Because privileges acquired via roles work at SQL level or in anonymous PL/SQL blocks, but won't work in named PL/SQL procedures or functions - and that's what you have, a procedure named get_magi_applicant_data.
So, what to do? Grant privileges directly.
As of another error you got:
Error(31,9): PLS-00201: identifier 'V_INSERT_OR_UPDATE' must be declared
Looks like it is about if v_insert_or_update is null then line. Error isn't obvious; there is v_insert_or_update local variable declared in the procedure, so I can't guess what might be wrong here.

Error: Could not use view or function because of binding errors

I got read only access to Views and when i am trying to query the View i got this error message. Can anyone help me understand what is the actual problem and how to fix it.
FYI.. this is the 1st time i am viewing this table .
Here is the error message.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'ProductCategoryL2Name'.
Could not use view or function 'DB.Product' because of binding errors.
It sounds like the view was created and then one of the underlying tables was changed. I.e., ProductCategoryL2Name no longer exists or was renamed. You can try this to get the view's definition, but the sys tables might be locked down. Your best bet is to go talk to whoever owns the database and ask them to fix it (which can be quite a rabbit hole in large organizations or on consulting gigs).
SELECT sm.definition
FROM [YourDB].sys.sql_modules AS sm
JOIN [YourDB].sys.objects AS o
ON sm.object_id = o.object_id
WHERE sm.object_id = OBJECT_ID('YourDB.dbo.ViewName')
To correct the wrong column name using the SSMS tool, right click the table with issue in the "View" folder then click "Design" and correct it from there, make sure to save when you click close.
when deploying changes to our systems it is not uncommon to get this error message Could not use view or function because of binding errors.
This happens because a view can become outdated because of changes to the underlying objects upon which the view depends.
you can use sp_refreshview to for correction - or better - prevention - if you do a sp_refreshview #viewname='the_view_I_have_just_changed' every time you change a view or function.
example:
use my_database
exec sp_refreshview #viewname='dbo.vw_select_applicant'
exec sp_refreshview #viewname='dbo.vw_search'
execute as login='cola'
--to check the permissions
select top 10 * from dbo.vw_select_applicant
select top 10 * from dbo.vw_search

Table is automatically truncated on SQL Server

I have a really strange problem on my SQL Server.
Every night 2 tables, that I have recently created, are being automatically truncated...
I am quite sure, that it is truncate, as my ON DELETE Trigger does not log any delete transactions.
Additionally, using some logging procedures, I found out, that this happens between 01:50 and 01:52 at night. So I checked the scheduled Jobs on the server and did not find anything.
I have this problem only on our production server. That is why it is very critical. On the cloned test server everything works fine.
I have checked transaction log entries (fn_dblog), but didnt find any truncate logs there.
I would appreciate any help or hints that will help me to find out process/job/user who truncates the table.
Thanks
From personal experience of this, as a first step I would look to determine whether this is occurring due to a DROP statement or a TRUNCATE statement.
To provide a possible answer, using SSMS, right click the DB name in Object Explorer, mouse over Reports >> Standard Reports and click Schema Changes History.
This will open up a simple report with the object name and type columns. Find the name of the table(s), click the + sign to expand, and it will provide you history of what has happened at the object level for that table.
If you find the DROP statement in there, then at least you know what you are hunting for, likewise if there is no DROP statement, you are likely looking for a TRUNCATE.
Check with below query,
declare #var as varchar(max)='tblname'
EXEC sp_depends #objname =#var;
it will return number of stored procedure name which are using your table and try search for any truncate query if you have wrote by mistake.
Thanks a lot to everyone who has helped!
I've found out the reason of truncating. It was an external application.
So if you experience the same problem, my hint is to check your applications that could access the data.
I don't know if can help you to resolve the question.
I often encounter the following situations.
Look at this example:
declare #t varchar(5)
set #t='123456'
select #t as output
output:12345

error while creating table in procedure [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i want to create a procedure in which i can create a table.
so i have done this
CREATE OR REPLACE PROCEDURE test_proc
BEGIN
execute immediate 'CREATE TABLE ABC_TABLE AS SELECT * FROM XYZ_TABLE WHERE 1=0';
END;
but after compiling i get following error.
Encountered the symbol "BEGIN" when expecting one of the following: ( ; is with authid as cluster compress order using compiled wrapped external deterministic parallel_enable pipelined The symbol "is" was substituted for "BEGIN" to continue.
can any one tell me reason for this and how to create the table in procedure.
thanks in advance...
You are missing "AS" between test_proc and BEGIN.
So It should be
CREATE OR REPLACE PROCEDURE test_proc AS
BEGIN

Check if table exists, before performing operations on it? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In searching for an answer to this question, I found this popular post on StackOverflow. Unfortunately, it doesn't work completely. The question is this:
Is there a way to check for existence of a table (or another object) before performing modifications (e.g. INSERT)? The before mentioned post suggests this:
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'questionableTable'))
BEGIN
INSERT INTO dbo.questionableTable VALUES ('success!');
END
Error: Invalid object name 'dbo.questionableTable'.
The problem with this is that SQL Server fails when it parses the INSERT statement, stating that dbo.questionableTable doesn't exist. The previous INFORMATION_SCHEMA check doesn't seem to affect it.
Is there a way to write this kind of query? For SQL Server, in particular. But I would also like to see similar operations for other database systems, if such things exist.
The motivation behind this question is because we have multiple databases which contain subsets of each others' tables. What I would like is to have a single script that can be applied to all databases, and which only modified the tables that exist there (and doesn't error upon execution).
Use dynamic SQL via the EXEC() function:
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'questionableTable'))
BEGIN
EXEC('INSERT INTO dbo.questionableTable VALUES (''success!'')');
END
The EXEC() function executes a string as SQL, but being a string it isn't evaluated until executed, so the tables mentioned in the string don't need to exist at compile time. This allows the stored proc to be defined prior to the table being created.
I tested this on my local server and it seems to work:
if exists (select * from dbname.sys.tables where name='tablename')
begin
select * from dbname.dbo.tablename
end