sql print returns strange result - sql

I have the following sql code:
DECLARE #result VARCHAR
SET #result = 'FN'
print(#result)
It returns F to console.
why?

If you define a VARCHAR, you have to specify it's length. If you don't it defaults to 1.
Use:
DECLARE #result VARCHAR(2)
SET #result = 'FN'
print(#result)

Related

Stored procedure returns 0 instead of value

There is a stored procedure that can return top 1 result as
USE [DB]
GO
.....
CREATE PROCEDURE [dbo].[GET]
(#in VARCHAR(10), #Out VARCHAR(10) OUTPUT)
AS
SELECT top 1 #Out = tab.Col
FROM table tab
RETURN
GO
When I call it in main query
DECLARE #output VARCHAR(10)
DECLARE #in VARCHAR(10)
DECLARE #Out VARCHAR(10)
EXECUTE dbo.GET #in = 'table', #Out = #output
It prints #output as 0;
but if I do
EXECUTE dbo.GET #in = 'table', #Out = #Out
And print #out, I get the correct value.
Why could this happen?
I did pass output #Out to pre-defined variable #output
Assuming SQLS due to presence of 'dbo' and sqlserver tag
Your query in the procedure doesn't assign a value to the out parameter (called #out) it assigns to some other variable called #outpk. Resolve the naming mismatch and make them the same
Sqlserver does not support LIMIT. To limit result set size use SELECT TOP 1 *. Using TOP (or any similar result set restrictor) without ORDER BY is illogical. Specify an ORDER BY
In sqlserver, output parameters must be passed with the OUTPUT keyword when calling the procedure:
EXEC sprocname #inputparameter ='value', #outputparameter = #variableToSet OUTPUT;
Use semicolons; omitting them is deprecated
Example
USE [DB]
GO
CREATE PROCEDURE [dbo].[GET]
(#in VARCHAR(10), #OutPk VARCHAR(10) OUTPUT)
AS
SELECT #OutPK = tab.Col
FROM table tab
ORDER BY tab.Col;
GO
DECLARE #output VARCHAR(10);
EXECUTE dbo.GET #in = 'table', #OutPK = #output OUTPUT
SELECT #output;
If its MySql (Limit is in mySql), you can simply call:
Call dbo.GET('table', #out);
No need to have separate variable #output.

Compare if textfield value is greater then x

How can I compare a numeric value in a textfield against any given value x?
Iam writing the code in TSQL.
See code below:
DECLARE #TA_SM varchar(5000)
DECLARE #TA_MSD varchar(5000)
DECLARE #T_SUM int(5)
SET #TA_SM = '[field name='Toelichting_advies_ISM']'
SET #TA_MSD = '[field name='Toelichting_advies_MSD']'
SET #T_SUM = '[field name='Offerte_totaalsom']'
IF ((#TA_SM = '') OR (#TA_MSD = '')) AND (#T_SUM >25.000)
SELECT 1
ELSE
SELECT 2
I get a error message saying:
Must declare the table value #T_SUM
The statement before the AND operator is working just fine.
Change the DECLARE #T_SUM int(5) to DECLARE #T_SUM int.
If you only want to have 5 digits use NUMERIC instead of INT.
EDIT 1 - Full code.
This should run through in TSQL without error but I am not sure if this is the result you are looking for. I have added extra quote marks to your variables definitions.
BEGIN
DECLARE #TA_SM varchar(5000)
DECLARE #TA_MSD varchar(5000)
DECLARE #T_SUM int
SET #TA_SM = '[field name=''Toelichting_advies_ISM'']'
SET #TA_MSD = '[field name=''Toelichting_advies_MSD'']'
SET #T_SUM = '[field name=''Offerte_totaalsom'']'
IF ((#TA_SM = '') OR (#TA_MSD = '')) AND (#T_SUM >25.000)
SELECT 1
ELSE
SELECT 2
END

Why do I have two different return values from the queries in T-SQL

Hello I wonder why I have 2 different return Values.
Here is the first query:
declare #currentcolumn_val varchar
declare #start integer
set #currentcolumn_val = 'state_val'
set #start =1
select #currentcolumn_val from z_skm where id = #start
the returned value is just "s"
Here is the second Query which gives the correct return value:
select state_val from z_skm where id = 1
This query gives me exactly what it should.
I hope you guys can help.
Cheers steven
Your first query is returning a constant. The second is returning the value of the column.
You are returning 's' instead of 'state_val' because of the declaration:
declare #currentcolumn_val varchar;
You have no length on varchar() and in this context, it defaults to a length of 1. Always use length with varchar() in SQL Server.
If you want the column to be dynamic, you need to use dynamic SQL:
declare #currentcolumn_val nvarchar(255);
declare #start integer;
declare #sql nvarchar(max) = 'select #currentcolumn_val from z_skm where id = #start';
set #sql = replace(#sql, '#currentcolumn_val', #currentcolumn_val);
exec sp_executesql #sql, N'#start int', #start = #start;
You can pass a parameter as an argument, but not a column or table name.
You have declared #currentcolumn_val simply as varchar, which defaults to a length of 1.
if you replace it with declare #currentcolumn_val varchar(10) you will see the whole value
First you haven't given the #currentcolumn_val parameter a size so it is equivalent to a VARCHAR(1). This means that this:
set #currentcolumn_val = 'state_val'
Is the essentially the same as:
set #currentcolumn_val = 's'
Then in your SELECT you are returning the value of that variable, not a column from the table.

Return a concatenated value from a SQL stored procedure

Please advise me to get the return value as 4/30 from a stored procedure.
I have declared a stored procedure with one output parameter #result varchar(5) output and no input parameter,
I have declared 3 variables inside a stored procedure say
declare #a float
declare #b float
declare #c varchar
set #a=4
set #b=30
set #c=cast(#a as varchar)+'/'+cast(#b as varchar)
set #result=#c
return #result
The stored procedure executes and returns the return value as 4.
But I need the return value to be 4/30. Is it possible to get 4/30 as return value?
Please suggest me on the above...
Actually I want to set #a to a select query which returns a float number,
and set #b to another select query which returns a float number .. but in the above sp just showed an example. Please advise me to get the return value as 4/30.
You need to specify a size for #c. Without size it will be varchar(1).
declare #c varchar(5)
You should specify the length parameter when declaring the variable #c
declare #c varchar(5)
After writing down query in MS,the thing i clear about is that there is need to specify variable length of variable.....
declare #a int;
declare #b int;
declare #c varchar(5);
set #a=4;
set #b=30;
SET #c=cast(#a as varchar)+'/'+cast(#b as varchar)
select #c;

How to user prefix 'N' for unicode with nvarchar variable in SQL Server?

How to user prefix 'N' for unicode with nvarchar variable in SQL Server? For example:
Given this variable:
declare #Query1 nvarchar(max)
I can assign to it like this:
set #Query1 = N'لاحظات'
But what if I want to use N#Query1 somewhere?
You only need to use N'xyz' when you have literal text. Once the nvarchar data is in a variable or result set column of nvarchar data type you no longer need to use the N'...' notation, the system knows the data type at that point.
try it out:
DECLARE #A nvarchar(100)
,#B nvarchar(100)
SET #A = N'anything here!!'
SET #B=#A --would work the same if coming from a query result set as well
SELECT #B --will show special unicode characters
declare #nv1 nvarchar(50) = N'لاحظات', #nv2 nvarchar(50) = 'لاحظات', #v varchar(50) = N'لاحظات'
declare #nv3 nvarchar(50) = #nv1 + ' hallo', #nv4 nvarchar(50) = #nv1 + N' hallo'
select #nv1, #nv2, #nv3, #nv4, #v
It is used with string literals to indicate the text should be treated as unicode. e.g.
DECLARE #something NVARCHAR(100)
SET #something = N'sometext'
Declare #var nvarchar(255)
Set #var = N'Hello World'
print #var
create table #tmp( columnA nvarchar(255) not null)
insert into #tmp(columnA) Values (N'Test')
Select #var = columnA from #tmp
print #var
drop table #tmp
Thanks to marc_s for his answer, that solved my problem.
I highlight it here as an answer for those who can't find it.
"
if #Query1 IS a NVARCHAR variable, then it IS a NVARCHAR variable and you don't need to prefix it with another 'N' to make it NVARCHAR.... just use it
"