views:

221

answers:

5

how to assign the integer value to nvarchar or varchar datatype in stored procedure

DECLARE @SQLString nvarchar(max)

SET @SQLString = N'declare @Identifier int;
                   SELECT  COUNT(*) FROM ' + @batch+' where Identifier = @Identifier'

i need to check whether the @SQLString is 0 or not.

i.e i want to check -----> if(@SQLString =0). how to assign a integer val to nvarchar or varchar

A: 

Hi,

convert it:

SET @var = CONVERT(VARCHAR, @intval)

Sascha

Sascha
If you want to check wether there's text in the variable, test for null and if not null use the LEN function.
Sascha
A: 

You're setting @SQLString to a query... if you want to see if it's '0', then you can just do:

IF @SQLString = '0'

But I think you're trying to find out if there are 0 rows in your batch, in which case, you mean something more like:

DECLARE @Res TABLE (cnt int);
INSERT @Res exec sp_executesql @SQLString;

IF (SELECT MAX(cnt) FROM @Res) = 0 /* Means empty batch */
Rob Farley
Number of rows *might* be in @@ROWCOUNT too.
Sascha
@@ROWCOUNT would be 1, for the count. It would have to be `SELECT 1 FROM theTable` to cause @@ROWCOUNT to be set.
Rob Farley
ya i got it. thank you guys
Ranjana
-1 because inserting a rowset into a table to get one output parameter (then querying it) is not good. sp_executesql supports output paramters. Use them.
Emtucifor
Yes, true. But it doesn't support table-valued parameters as output (yet), and I like this pattern for its ability to pull back whole sets. I see this behaviour put into loops for single results, and I really don't like that. I definitely agree that this particular case suits the output parameter.
Rob Farley
A: 

I am assuming that you are trying to check how many rows the dynamic sql returned you can do something like this:

DECLARE @SQLString nvarchar(max) 

SET @SQLString = N'declare @Identifier int; 
                   SELECT  * FROM ' + @batch+' where Identifier = @Identifier'

exec @SQLString

set @SQLString = @@ROWCOUNT

if @SQLString = 0
begin
...
end
Jose Chama
+1  A: 

You could try something like

DECLARE @IntVal INT,
        @ParamDef NVARCHAR(MAX),
        @SQLString nvarchar(max),
        @batch VARCHAR(MAX)

SELECT  @batch = 'Batch',
        @SQLString = N'SELECT @IntVal = COUNT(*) FROM ' + @batch,
        @ParamDef = '@IntVal INT OUTPUT'

EXECUTE sp_executesql  @SQLString,@ParamDef, @IntVal=@IntVal OUT

SELECT @IntVal

Have a look at sp_executesql (Transact-SQL)

astander
+1 for sp_executesql with an output parameter. -1 for not suggesting to stop using Count() just to check if there are no rows. -0.49 for declaring the very simple @ParamDef that will always be a simple literal string. Rounding = 0.
Emtucifor
+1  A: 

I think this way is best:

DECLARE
   @Cnt int,
   @SQL nvarchar(max),
   @batch sysname,
   @Identifier varchar(30)

-- set @batch and @Identifier

SET @SQL = 'SELECT @Cnt = Count(*) FROM ' + @batch
   + ' WHERE Identifier = @Identifier'
EXEC sp_executesql @SQL, N'@Cnt int OUT, @Identifier varchar(30)',
   @Cnt OUT, @Identifier
IF @Cnt = 0 BEGIN
   --do something
END
ELSE BEGIN
   --do something else
END

Though if you only care whether it's 0 or not, then you should do this instead, which can stop after finding only one row, instead of having to count them all:

DECLARE
   @HasRows bit,
   @SQL nvarchar(max),
   @batch sysname,
   @Identifier varchar(30)

-- set @batch and @Identifier

SET @SQL = 'SET @HasRows = CASE WHEN EXISTS (SELECT 1 FROM '
  + @batch + ' WHERE Identifier = @Identifier) THEN 1 ELSE 0 END'
EXEC sp_executesql @SQL, N'@HasRows bit OUT, @Identifier varchar(30)',
   @HasRows OUT, @Identifier
IF @HasRows = 0 BEGIN
   --do something
END
ELSE BEGIN
   --do something else
END

However, if there's any way you can avoid using dynamic SQL and changing table names, that would be best. Then it is a simple query:

IF NOT EXISTS (SELECT 1 FROM TableName WHERE Identifier = @Identifier) BEGIN
   -- do something
END
Emtucifor