Error Incorrect syntax - tsql

I have created a procedure in Sybase
create procedure prcrms_crms_cust_id_verify_ins(#customer_code numeric(12,0),#id_type tinyint,#verification_status varchar(12),#verification_response varchar(255),#verification_date datetime,#add_user varchar(21)) as
begin
declare #check_id_exists int
select #check_id_exists = count(*) from crms_customer_id_verification where ( ('customer_code'=#customer_code) and ('id_type'=#id_type))
if(#check_id_exists > 0)
begin
update crms_customer_id_verification set verification_status=#verification_status,verification_response=#verification_response,verification_date=#verification_date where ( ('customer_code'=#customer_code) and ('id_type'=#id_type))
return ##rowcount
end
if((#check_id_exists <> null) or (#check_id_exists <> 0))
begin
insert into crms_customer_id_verification(customer_code,id_type,verification_status,verification_response,verification_date,add_user) values(#customer_code,#id_type,#verification_status,#verification_response,#verification_date,#add_user)
return ##rowcount
end
end
when i try to execute the procedure
exec prcrms_crms_cust_id_verify_ins(3344,0,"VERIFIED",'{test:test}','1998-09-09 12:12:12.000','Admin')
it shows Incorrect syntax near '3344'.

/*
create procedure prcrms_crms_cust_verify_ins(#customer_code varchar(255),#id_type varchar(255) ,#verification_status varchar(12),#verification_response varchar(255),#verification_date varchar(255),#add_user varchar(21))as
/*
** ------------------------------------
** Created By : Bibil Mathew Chacko
** Created On : Jul 16 2016
** Description : insert in crms_customer_id_verification if id_type and customer exist.Update table if customer and id_type exist.
** -------------------------------------
*/
begin
declare
#check_id_exists int,
#cc numeric,
#id_t tinyint,
#vd datetime
select #cc = convert(numeric(12,0),#customer_code)
select #id_t = convert(tinyint,#id_type)
select #vd = convert(datetime,#verification_date)
select #check_id_exists = count(*) from crms_customer_id_verification where ( (customer_code=#cc) and (id_type=#id_t))
if(#check_id_exists > 0)
begin
update crms_customer_id_verification set verification_status=#verification_status,verification_response=#verification_response,verification_date=#vd where (customer_code=#cc and id_type=#id_t)
return ##rowcount
end
if((#check_id_exists <> null) or (#check_id_exists <> 0))
begin
insert into crms_customer_id_verification(customer_code,id_type,verification_status,verification_response,verification_date,add_user) values(#cc,#id_t,#verification_status,#verification_response,#vd,#add_user)
return ##rowcount
end
end
*/
/*exec prcrms_crms_cust_verify_ins '3344','0',"VERIFIED",'{test:test}','1998-09-09 12:12:12.000','Admin'*/
Should use VARCHAR type as parameter and procedure should have support for converting to required type. Another mistake I have done is Sybase Procedures don't have parenthesis.

Related

PostgreSQL - SQL state: 42601 syntax error Character: 316

I would like to know how to resolve this syntax error. I've tried lots of ways, however, when I try to compile my function a message SQL 42601 is displayed.
The code that I use:
create or replace PROCEDURE SP_Generate_EventToken(EventId bigint) AS $$
DECLARE TokenCount int;
DECLARE Success int;
DECLARE SerialNo int=0;
BEGIN
BEGIN TRY
SET #Success = 0;
--Get Available Token count
SELECT #TCNT = COUNT(Field1) FROM TbECT WHERE Field2 = 0;
IF EventId > 0
BEGIN
SELECT SerialNo=(SerialNo) from TbDefault
--Get Generated Token Count
SELECT #TokenCount = COUNT(Id) FROM TbComplementaryClaim WHERE [EventId] = EventId
--Get Guest Count
SELECT #GUESTNO = (CASE WHEN TokenType = 0 THEN [NoOfGuests] WHEN TokenType = 1 THEN 1 WHEN TokenType = 2 THEN [NoOfGuests] END), #TokenType = TokenType, #GUESTNOC = [NoOfGuests] FROM [dbo].[TbComplementaryEvents] WHERE [Id] = #EventId
SET #GUESTNO = #GUESTNO - #TokenCount
IF #GUESTNO > 0
BEGIN
BEGIN TRANSACTION
--Reset all token
IF #TCNT < #GUESTNO
BEGIN
UPDATE TbECT SET Field2 = 0
END
COMMIT TRANSACTION
END
END
END TRY
END;
$$ LANGUAGE plpgsql;
Error message I receive:
ERROR: syntax error at or near "TRY"
LINE 14: BEGIN TRY;
^
SQL state: 42601
Character: 316
What is wrong? How can I solve this problem?

Return both out value and query select value in postgres

Here is my sql server sample procedure while migrating to postgres sql i am unable to return output value and
return query value.I want to return SUCCESS and followed by query value
ALTER PROCEDURE [dbo].[VERIFY](
#P_VER_NO VARCHAR(5) ,
#P_SOLID VARCHAR(6) ,
#P_ID VARCHAR(7) ,
#P_MOBNO VARCHAR(15) ,
#P_OUT_MSG VARCHAR(2) OUT
)
AS
BEGIN
DECLARE #VOTP INT , #V_LNK INT ,#V_VERAVL VARCHAR(2)
IF #VOTP=0
BEGIN
SELECT #P_OUT_MSG='SUCCESS'
SELECT #P_OUT_MSG
SELECT MCI.CHQ_MIN_LGTH AS MINCHEQUELENGTH,
MCI.VRTL_CARD AS VIRTUALCARD,
FROM TRAN_TABLE MCI WITH(NOLOCK)
WHERE MCI_SOLID=#P_SOLID;
RETURN
END
ELSE
begin
SELECT #P_OUT_MSG='FAILURE'
SELECT #P_OUT_MSG
SELECT MCI.ACCT_STAT AS MINCHEQUELENGTH,
MCI.LOG_ATMPT AS VIRTUALCARD,
FROM TRAN_TABLE MCI WITH(NOLOCK)
WHERE MCI_SOLID=#P_SOLID;
RETURN
end
END
You should have two out argument one for success/failure, and other for the value.
Or you should not have an out argument for success/failure, but RAISE ESCEPTION on failure...

TFS 2013 Update 4 to TFS 2015 Update 3 Collection Error

Updating my TFS 2013 Update 4 collection to TFS 2015 Update 3. Using a backup of the production collection data in a DEV location. Did the backup with the production collection being detached. Didn't have any errors. The backup is 254GB.
This is the error currently stopping me from attaching the collection:
Msg 3732, Level 16, State 1, Line 93
Cannot drop type 'typ_ItemSpec2' because it is being referenced by object 'prc_QueryPendingChanges_MS'. There may be other objects that reference this type.
SET XACT_ABORT ON
SET NOCOUNT ON
DECLARE #status INT
DECLARE #procedureName SYSNAME = N'upd_VersionControlToDev14M80_PostSchema'
DECLARE #tfError NVARCHAR(255)
IF EXISTS (
SELECT *
FROM sys.triggers
WHERE name = 'trg_tbl_VCFirstRunProject'
)
BEGIN
DROP TRIGGER trg_tbl_VCFirstRunProject
END
IF EXISTS (
SELECT *
FROM sys.indexes
WHERE name = 'IX_tbl_VCFirstRunProject_OldServerItemPrefix'
AND object_id = OBJECT_ID('dbo.tbl_VCFirstRunProject')
)
BEGIN
-- Delete upgrade-only rows for $\, a few partitions at a time
-- We need dynamic SQL for this to be rerunnable.
EXEC #status = sp_executesql N'
DECLARE #batchStart INT = 1
DECLARE #batchEnd INT
DECLARE #end INT
DECLARE #batchSize INT = 50
-- Get the partition range
SELECT TOP (1)
#end = PartitionId
FROM tbl_VCFirstRunProject
ORDER BY PartitionId DESC
WHILE (#batchStart <= #end)
BEGIN
SET #batchEnd = #batchStart + #batchSize
DELETE tbl_VCFirstRunProject
WHERE PartitionId BETWEEN #batchStart AND #batchEnd
AND OldServerItemPrefix = N''''
OPTION (OPTIMIZE FOR (#batchStart=1, #batchEnd=50))
SET #batchStart = #batchEnd + 1
END
'
IF (#status <> 0)
BEGIN
SET #tfError = dbo.func_GetMessage(500004); RAISERROR(#tfError, 16, -1, #procedureName, #status, N'sp_executesql', N'DELETE tbl_VCFirstRunProject')
RETURN
END
DROP INDEX IX_tbl_VCFirstRunProject_OldServerItemPrefix ON tbl_VCFirstRunProject
END
IF EXISTS (
SELECT *
FROM sys.columns
WHERE object_id = Object_ID(N'dbo.tbl_VCFirstRunProject', N'U')
AND name = N'OldServerItemPrefix'
)
BEGIN
ALTER TABLE tbl_VCFirstRunProject
DROP COLUMN OldServerItemPrefix, NewServerItemPrefix
END
IF TYPE_ID('dbo.typ_BranchObject2') IS NOT NULL
BEGIN
DROP TYPE typ_BranchObject2
END
IF TYPE_ID('dbo.typ_BuildMappingInput2') IS NOT NULL
BEGIN
DROP TYPE typ_BuildMappingInput2
END
IF TYPE_ID('dbo.typ_CreateLabelInput') IS NOT NULL
BEGIN
DROP TYPE typ_CreateLabelInput
END
IF TYPE_ID('dbo.typ_ExpandedChange2') IS NOT NULL
BEGIN
DROP TYPE typ_ExpandedChange2
END
IF TYPE_ID('dbo.typ_ItemSpec2') IS NOT NULL
BEGIN
DROP TYPE typ_ItemSpec2
END
IF TYPE_ID('dbo.typ_LocalPendingChange3') IS NOT NULL
BEGIN
DROP TYPE typ_LocalPendingChange3
END
IF TYPE_ID('dbo.typ_LocalVersion3') IS NOT NULL
BEGIN
DROP TYPE typ_LocalVersion3
END
IF TYPE_ID('dbo.typ_LockConflictCandidate2') IS NOT NULL
BEGIN
DROP TYPE typ_LockConflictCandidate2
END
IF TYPE_ID('dbo.typ_LockObject') IS NOT NULL
BEGIN
DROP TYPE typ_LockObject
END
IF TYPE_ID('dbo.typ_Mapping2') IS NOT NULL
BEGIN
DROP TYPE typ_Mapping2
END
IF TYPE_ID('dbo.typ_PendingAdd2') IS NOT NULL
BEGIN
DROP TYPE typ_PendingAdd2
END
IF TYPE_ID('dbo.typ_PendingChangeObject') IS NOT NULL
BEGIN
DROP TYPE typ_PendingChangeObject
END
IF TYPE_ID('dbo.typ_PendingChangeSecurity') IS NOT NULL
BEGIN
DROP TYPE typ_PendingChangeSecurity
END
IF TYPE_ID('dbo.typ_PendingMerge2') IS NOT NULL
BEGIN
DROP TYPE typ_PendingMerge2
END
IF TYPE_ID('dbo.typ_PendingPropertyChange2') IS NOT NULL
BEGIN
DROP TYPE typ_PendingPropertyChange2
END
IF TYPE_ID('dbo.typ_VersionedItemId') IS NOT NULL
BEGIN
DROP TYPE typ_VersionedItemId
END
None of the standard queries from Microsoft have _MS as a suffix. I suspect that someone hand-tweaked the original prc_QueryPendingChanges and left this around as a backup. In that case you should be able to drop this procedure and retry the upgrade.

How do I restrict the number of rows in a table for a specific column value?

I'm trying to do this in postgreSQL:
You can't store more than 5 records (rows).
CREATE FUNCTION contar_dest()
RETURNS TRIGGER AS
$body$
BEGIN
IF (SELECT COUNT(ID) FROM "LUGARES" WHERE demanda is not null) > 5
THEN
DELETE FROM "LUGARES"
WHERE ID = (SELECT max(ID) FROM "LUGARES");
RETURN NULL;
END IF;
END;
$body$
LANGUAGE plpgsql;
CREATE TRIGGER contar
AFTER INSERT
ON "LUGARES"
FOR EACH ROW
EXECUTE PROCEDURE contar_dest();
When I try to insert a row show me this:
ERROR: execution reached the end of trigger procedure without finding RETURN
CONTEXT : PL / pgSQL function contar_dest ()
Now I do what I want but the error is this now:
CREATE FUNCTION contar_dest()
RETURNS TRIGGER AS
$body$
BEGIN
IF ((SELECT COUNT(ID) FROM "LUGARES" WHERE demanda is not null) > 5) THEN
DELETE FROM "LUGARES"
WHERE ID = (SELECT NEW.ID FROM "LUGARES");
ELSIF ((SELECT COUNT(ID) FROM "LUGARES" WHERE demanda is not null) < 5) THEN
RETURN NULL;
END IF;
RETURN NULL;
END;
$body$
LANGUAGE plpgsql;
CREATE TRIGGER contar
AFTER INSERT
ON "LUGARES"
FOR EACH ROW
EXECUTE PROCEDURE contar_dest();
INSERT INTO "LUGARES"(
nombre, demanda)
VALUES ('Valencia',2000);
ERROR: a subquery used as an expression returned more than one record
CONTEXT : SQL statement : " DELETE FROM "LUGARES"
WHERE ID = ( SELECT FROM NEW.ID "LUGARES") ยป
PL / pgSQL contar_dest ( ) function in line 4 SQL statement
********** Error **********
The error is caused by you using syntax that requires maximum of 1 row, but when executed returns many.
Change:
DELETE FROM "LUGARES"
WHERE ID = (SELECT NEW.ID FROM "LUGARES");
To:
DELETE FROM "LUGARES"
WHERE ID IN (SELECT NEW.ID FROM "LUGARES");
= only works with 1 value, but IN allows many values.

while loop in procedure sybase

In oracle when we write a procedure we do it like this :
COUNT_START:= LL_COUNT;
WHILE LL_COUNT > 0 LOOP
IF LL_COUNT > 20 THEN
COUNT_START := 20;
ELSE
COUNT_START := LL_COUNT;
LL_COUNT := 0;
END IF;
I am writing a procedure in sybase :
CREATE PROCEDURE P_RDS_EOD_ARCH_PURGE
v_test numeric(10,0)
AS
BEGIN
DECLARE #ID VARCHAR(200)
DECLARE #NAME VARCHAR(200)
DECLARE #TYPE VARCHAR(200)
DECLARE #SQL_TXT VARCHAR(255)
SELECT #ID = '1'
SELECT #NAME = 'P_TEST'
SELECT #TYPE = 'SELECT'
SELECT #SQL_TXT ='RANDOM QUERY'
EXECUTE #ID, #NAME , #TYPE, #SQL_TXT
After I execute my query I want to do a LOOP and to assign values, how to do that in sybase
Loops are pretty basic in TSQL:
while boolean_expression
statement
If your statement is more than 1 line, then you can enclose it in begin/end statements;
while aStatementIsTrue
begin
update myTable
set myCol = "whatever"
select someOtherThing
end
More details can be found in the Sybase T-SQL User's Guide.