Sybase SQL QUERY - error while executing stored procedure - sql

When I try to execute this stored procedure im getting this error"Database Connector Error: 'Implicit conversion from datatype 'CHAR' to 'INT' is not allowed. Use the CONVERT function to run this query" how come? how can I fix this? thanks
this is the query im using to execute the SP:
CREATE PROCEDURE TestProc2(#store char(4))
AS
BEGIN
SELECT * FROM stores WHERE stor_id=#store
END
exec TestProc2 #store = "7066"
sp_help stores
stor_id, char, 4
stor_name, varchar, 40
stor_address,varchar, 40
city, varchar, 20
state, char, 2
country, varchar, 12
postalcode, char, 10
payterms, varchar, 12
the column is set as char(4) so I don't think there is a mismatch with the query and data types
BTW I tried("7066") with single quotes, with double and without quotes, I still get the same sql error. please help
PS Im using sybase thanks! also below is a screenshot of the error.
any other info you guys need to help me? thanks
http://tinypic.com/r/2n24huq/8

It looks like the stor_id field in the stores table is an int. Change your stored procedure to accept an int instead of a char(4).
CREATE PROCEDURE TestProc2(#store int)
AS
BEGIN
SELECT * FROM stores WHERE stor_id=#store
END

Related

Failed to execute query. Error: String or binary data would be truncated in table xdbo.user_info', column 'uid'

I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE

How to call a DB2 AS400 table valued function which take Date parameters

I have a table valued function as below
CREATE FUNCTION ABELIBLE.TVFBOEGETSHIPMENTS (STARTDATE DATE, ENDDATE DATE, ADDRESSCODE CHAR(9))
RETURNS TABLE (ID INT, JOBNUMBER CHAR(9), CUSTOMERREFERENCE CHAR(18),
CONSIGNEENAME CHAR(30), CREATEDDATE DATE, AIRPORTOFORIGIN CHAR(3), AIRPORTOFARRIVAL CHAR(3),
AIRPORTOFDESTINATION CHAR(3), COUNTRYOFDESTINATION CHAR(3), ADDRESSCODE CHAR(9), CONSIGNMENTNUMBER CHAR(25))
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN
SELECT
ROW_NUMBER() OVER(ORDER BY EMJOBN DESC),
A.EMJOBN,
A.EMCREF,
A.EMOSNM,
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')),
A.EMAOFO,
A.EMAOFA,
A.EMAOFD,
A.EMCOFD,
A.EMUKCD,
A.EMRPRT
FROM DTALIBLE.EMASTER A WHERE A.EMPSFT = 'Y' AND A.EMUKCD = ADDRESSCODE AND
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) >= DATE(STARTDATE) AND DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) <= DATE(ENDDATE)
However I am not getting any results back when I run the following query
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
I am using DBeaver to run the query on AS400.
For my own sanity I tired the select statement with literals and i got the following result
Then again when I tried with below dates it does not fetch any results and i get this error.
any help is much appreciated. Thank you in advance.
The only thing that jumps out at me is that in your invocation,
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
'MUL0044' as a literal is treated as varchar, and your UDTF has the parm defined as char.
That mismatch usually gives a Function not found error however. You can cast the literal to char or change the parm type to varchar. Which to chose depends on how it will normally be invoked. If you're just testing with a literal and the function would normally be passed a char then just cast the literal to char.
If you use the Run SQL Scripts component of IBM ACS, you'll have the ability to debug the function.

Why this simple stored procedure isn't working

Here is the deal, I am receiving an array from C# and I want to insert it into the following table with only 2 columns which are #idUser int and #idRegion int.
The stored procedure needs to receive the array and insert it into the table but somehow it isn't working, it tells me that it cannot convert #idRegion to an int. I tried to use CAST and CONVERT to convert it into int but it isn't working.
The Select From works ok, but not the insert.
Here is the stored procedure (#idUser needs to be the same for all inserted rows):
#idUser int,
#idRegion nvarchar(MAX)
AS
BEGIN
INSERT INTO [UsersRegion] (idUser,IdRegion)
VALUES (#idUser, #idRegion)
SELECT #idUser,cast(value as int) FROM STRING_SPLIT(#idRegion,',')
END
I get this error when running it:
Conversion failed when converting the nvarchar value '1,2,3,4' to data type int.
If you are sending multiple values in #idRegion then when you split them, you may have more than 1 things you need to insert. Therefore, do it like this:
INSERT INTO [UsersRegion] (idUser,IdRegion)
SELECT #idUser, value FROM STRING_SPLIT(#idRegion, ',')
If the target table's IdRegion column is of type int, you need to cast like this:
SELECT #idUser, cast(value as int) FROM STRING_SPLIT(#idRegion, ',')
Above code will insert the same #idUser for every record but a different value for IdRegion depending the splitted items. More on Insert into select from
Your INSERT statement seems to be working with IdRegion while everything else is lowercase id.
However, assuming this is how the actual table column is named and is not a typo...
Your problem is most likely the line that reads:
#idRegion nvarchar(MAX)
Which is declaring the #idRegion variable as a string, while you have stated in the question that it's meant to be an int.
This would explain the casting error.
If you cannot pass it into the procedure as an int from the C# code. Your only other option would be to try to parse it into an int as you have said.

Unable to delete right to left language columns using stored procedure

I'm using stored procedure to delete a row from MSSQL database based on a column that uses nvarchar(100) and Persian language.
when i want to insert into this column, i use the word N before the record to be able to perform the insert operation :
insert into materialPrice values( N'persian word',1000,100,0,0,0,0)
the problem is when i want to delete the same record, stored procedure does not work :
create proc spRemoveMaterial
#materialName nvarchar(100)
as
begin
delete from materialPrice where materialName = #materialName
end
I've tried to use N before #materialName but it returend syntax error. how could it be done ?
The N is a marker that causes the string literal to be represented in Unicode--implying that you are inserting into a Unicode column.
You should be able to convert the variable to Unicode with cast. Something like:
cast(#materialName as nvarchar(100))
With the correct type (nchar or nvarchar) and length to match the column.
The problem was with the database collation, following code has fixed it :
ALTER database MaterialDB COLLATE Persian_100_CI_AS

Stored procedure in SQL used with substring

I have this stored procedure here:
else if(substring(#SMS,1,1)='S')
begin
insert into WEB_POROSIA..SMS_SERVISI(IDTICKET, MBYLLUR) values(convert(int,substring(#SMS,2,len(#sms)-1)),1)
select #sms
end
What it does is: I send a SMS like this:
S 23 and in the database it saves the 23 value..
Now, it works like this but not if I add a letter before:
i.e
S B21 it should insert B21 to the table...
How to modify it?
values(convert(int,substring(#SMS,2,len(#sms)-1)),1)
^ Because you are converting it into int
You can convert into varchar if you to get B
values(convert(varchar,substring(#SMS,2,len(#sms)-1)),1)
(Assuming you will alter the table and change the datetype from int to varchar)
You will need to remove the conversion to int:
insert into WEB_POROSIA..SMS_SERVISI(IDTICKET, MBYLLUR) values(substring(#SMS,2,len(#sms)-1),1)
You may also need to modify the type on the target column if that is also using int.