why it doesnt accept a concated string as parameter in SP? - sql

I wrote an SP that works perfectly. I was calling the SP with regular parameters and worked. However, now I need to call the SP like below
EXEC #v_nReturn = sp_get_next_value 'LP_' + #WhID + '_COUNTER'
The intellisense gives an error on the first '+' sign.
It says "incorrect syntax err...."
#WhID is NVARCHAR(10), so I shouldn't convert it to NVARCHAR.
what is the problem?

SQL Server doesn't do (full) expression parsing when you call a stored procedure. This is definitely an area where a small change would be highly convenient, although there are probably good reasons for the limitation.
As mentioned in a comment, use a separate variable:
DECLARE #arg varchar(256) = 'LP_' + #WhID + '_COUNTER';
EXEC #v_nReturn = sp_get_next_value #arg;
Be careful if #WhID is numeric. Then you need to convert the value to a string first.

Try with parens:
EXEC #v_nReturn = (sp_get_next_value 'LP_' + #WhID + '_COUNTER')

Related

SQL query error in a stored procedure in Microsoft SQL Server 2012

I am getting this error:
Incorrect syntax near the keyword 'set'.
Here is the code snippet
'Update ' + #TableName +' set status='+str(#status)+ ' where id in (Select Sid from '+#tname+' where SiteId=' + str(#SiteId) + ' and OtId = '+str(#OtId) + ' and (coalesce(VID,'''')='''' OR VID = ''' + #VID +'''))'
You are likely getting leading white spaces using the STR() function without specifying the length and this is causing an error. For example select str(1) returns 1 with 9 padded spaces.
What also could be happening is that #status is > 10. In this case it will be truncated to ****. For example,select str(12345678910) returns ***.
So, don't use STR() for this. Use CAST() or CONVERT(). Granted this may not be your only issue, but I suspect it is based on what you have provided. I would read the comments, as I agree that you have not given sufficient information to debug this code. Pay close attention to #Back's comment and use PRINT(#yourQuery) next time
MSDN on STR()
The specified length should be greater than or equal to the part of
the number before the decimal point plus the number's sign (if any). A
short float_expression is right-justified in the specified length, and
a long float_expression is truncated to the specified number of
decimal places. For example, STR(12,10) yields the result of 12. This
is right-justified in the result set. However, STR(1223,2) truncates
the result set to **.

Converting Varchar to Datetime for Stored procedure input

I want to use a datetime parameter in a stored procedure, in T-SQL, that if NULL will revert to 12/31/9999.
My code looks like this:
EXEC abc.someStoredProc #i_param1, #i_param2, ISNULL(#l_Termination_date, '12/31/9999')
I get an error:
Incorrect syntax near '#l_Termination_date'
I've tried using convert and cast (for example:
ISNULL(#l_Termination_date,CAST('12/31/9999' AS datetime))
but can't seem to get it right. What am I doing wrong?
You can pass variables or literals as arguments to execute a stored procedure, (or call one of a few specific functions), but what you can't do is have arbitrary expressions.
Move it to a separate step:
SET #l_Termination_date = ISNULL(#l_Termination_date,'99991231')
EXEC abc.someStoredProc
#i_param1
, #i_param2
,#l_Termination_date
(Or use a separate variable if you don't want to overwrite #l_Termination_date)
DECLARE #l_Termination_date DATE ;
SET #l_Termination_date = ISNULL(#l_Termination_date,'12/31/9999')
EXEC dbo.USP_abc.someStoredProc (
#i_param1
, #i_param2
,#l_Termination_date )

Convert Password varchar() to binary datatype in sql

I want to take password from Table 1 to Table 2. So I have to convert.
Table 1:
Password(varchar)
Table 2:
Password(binary)
I am logging here convert varchar to binary.I try this following query,
IsNull(''''+cast(wl.password as binary)+'''', 'NULL')+')'
but no use. It is showing error like,
The data types varchar and binary are incompatible in the add operator.
give me some suggestions?
Try using convert function in sql.
Eg.
convert(varchar, ' + #password + ')
Refer:
SQL server + dynamic query + 'The data types nvarchar and bit are incompatible in the add operator.'
Hope its helpful.
Don't cast it to binary before concatenating a string, but within the string, so it get casted when dynamic query is executed.
Something like:
ISNULL('cast(''' +wl.password+ ''' as binary)','NULL')

Sybase convert issue from float to varchar

First, I need to mention that my current sybase db version is Adaptive Server Enterprise 12.5.4. I aim to convert float data type into varchar via sybase convert function in order to concat several of these kinds of variables, format and store in string type.
Unfortunately, it is not the case. Simply using convert(varchar(20), float_var) or cast() function cannot correctly return the precise value.
For example, ...
declare #float_var float
begin
select #float_var =345.1237 --from table actually
select convert(varchar(20),#float_var) --return 345.1236999999
end
The incorrect string results returned occasionally have 99999 or 00001 suffix.
I tried many function including specify the precision, but there are still several cases not working on it. The sybase internal function does not exactly work on it.
I suppose this is a gerneral issue while using Sybase DB, however few answer found in serach. During my past experience, Sybase store procedure gammer always has sort of tolerance in runtime and internal fix when error encounter. This issue make me confused how Sybase works internally. Any advice would be appreciated, thanks in advance.
there are a couple of possible solutions for this.
firstly, let's try to convert the float to decimal first, then to varchar.
select cast(cast(#float_var as decimal(13,4)) as varchar)
alternatively, and this is where my ASE memory might fail me a little, would be to use the STR function like so:
Select ltrim(str(#float_var, 25, 5))
You have to TRIM the output as the STR function padding empty spaces on to the left of the result
this works for me:
declare #float_var float
begin
select #float_var = 96.332
select cast(cast(#float_var as decimal(13,4)) as varchar) -- Returns 96.3320
end
declare #float_var float
begin
select #float_var = 345.1237
select cast(cast(#float_var as decimal(13,4)) as varchar) -- Returns 345.1237
end

Using LOCATE string function in stored procedure

Is anyone familiar with using the LOCATE with a passed in string varialbe? I am trying to use it to determine if there is a comma in a string that was set in the stored procedure but have not been able to get it to work properly. My code looks something like this
DECLARE string VARCHAR(10);
DECLARE comma_found INT;
SET string = 'hello, world';
SET comma_found = SELECT LOCATE(',',string);
IF( comma_found <> 0 ) THEN
...execute code....
END IF;
This code will not complie because of the SELECT LOCATE and I can not figure out what is wrong. Is it my syntax? Usage? Is there any other string maniuplation function I can use to accomplish this? I am doing this within a stored procedure in Mysql.
I haven't used MySQL in a long time so I'm not completely familiar with the newer syntax but from some googling, I believe
SET comma_found = SELECT LOCATE(',',string);
should be
SELECT #comma_found := LOCATE(',',string);
then you can use #comma_found eg:
SELECT #comma_found
It seems that you're trying to assign a data set to the variable rather than the result of the LOCATE
See here: http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
Well the syntax for LOCATE is LOCATE(substring, string). Your query literally says LOCATE(',', string) where string is a varchar(10). Your string is actually more than 10 characters long though =P