Conversion failure varchar to int

  • How do I pass a varible to an INT calculation?

    CREATE PROC CLEAR_MY_TABLE

    @TableStat varchar(30)

    AS

    If OBJECT_ID('MyDB.dbo.' + @TableStat + '') is not null

    BEGIN

    ---PRINT 'I FOUND THE TABLE'

    DECLARE @count INT = -1;

    SELECT @count = (Select COUNT(*) FROM [dbo]. + @TableStat);

    IF (@count > 0)

    BEGIN

    TRUNCATE TABLE @TABLESTAT

    END

  • Why do you want to count before truncation? That would only slow you down. Additionally to that, you can't use variable values as table names unless you use dynamic SQL. Same goes for other statements.

    Here's an alternative:

    CREATE PROC CLEAR_MY_TABLE

    @TableStat nvarchar(128)

    AS

    DECLARE @sql nvarchar(200);

    SELECT @sql = 'TRUNCATE TABLE ' + QUOTENAME(@TABLESTAT);

    IF OBJECT_ID( QUOTENAME(@TABLESTAT) ) is not null

    EXEC( @sql );

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I'm still getting an error. I added the truncate command just to give an idea of what I was trying to do.

    "Conversion failed when converting the nvarchar value 'Select COUNT(*) FROM [AdventureWorks2014].[Production].[Product]"

    Declare @count int = -1

    Declare @TableStat NVARCHAR(128)

    SET @TableSTAT = 'Product'

    /* Verify the user typed the table name correctly and it exist in the database */

    If OBJECT_ID('AdventureWorks2014.Production.' + @TableStat + '') is not null

    BEGIN

    /* Now we know the table exist. Does it have any data? */

    /* If no data exist exit query */

    PRINT 'I FOUND A VALID TABLE WITH THE NAME SUPPLIED'

    --DECLARE @count NVARCHAR(128)

    DECLARE @params NVARCHAR(MAX)

    Set @count = N'Select COUNT(*) FROM [AdventureWorks2014].[Production].' + QUOTENAME(@TableStat)

    EXEC (@count)

    If ((@count) > 0)

    BEGIN

    PRINT 'I FOUND THE TABLE AND IT HAD MORE THAN 1 ROW'

    END

    END

    ELSE BEGIN

    PRINT '* * * THE TABLE NAME SUPPLIED IS NOT VALID. PLEASE CHECK THE SPELLING * * *'

    END

  • You can't do: SELECT @count = (Select COUNT(*) FROM [dbo]. + @TableStat);

    You need to build a string that has the query you want to execute, then execute that string.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Your variable is declared as integer and you're assigning a string value. Read about sp_executesql to understand how to use parametrized dynamic queries. Using parametrized queries, you can use an output parameter that gets populated in your dynamic code.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • You should read this article about dynamic sql. http://www.sommarskog.se/dynamic_sql.html

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Two side notes:

    1. A table with one row will return the message that has more than 1 row. :w00t:

    2. If you don't care about the number of rows in the table and just want something like true or false, you can use a faster approach.(I'm not sure that the table is correct)

    DECLARE @C int

    SELECT @C = 1 WHERE EXISTS(SELECT * FROM [AdventureWorks2014].[Production].[Product])

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Might as well handle any db and any schema, but defaulting to your main one.

    CREATE PROC CLEAR_MY_TABLE

    @TableStat varchar(100)

    AS

    SET NOCOUNT ON;

    DECLARE @TableStat varchar(100)

    SET @TableStat = ISNULL(PARSENAME(@TableStat, 3), 'MyDB') + '.' + ISNULL(PARSENAME(@TableStat, 2), 'dbo') + '.' + PARSENAME(@TableStat, 1)

    IF OBJECT_ID(@TableStat) IS NOT NULL

    BEGIN

    ---PRINT 'I FOUND THE TABLE'

    DECLARE @row_found tinyint

    DECLARE @sqln nvarchar(4000)

    SET @sqln = 'SELECT @row_found = CASE WHEN EXISTS(SELECT TOP (1) * FROM ' + @TableStat + ') THEN 1 ELSE 0 END'

    EXEC sp_executesql @sqln, N'@row_found tinyint OUTPUT', @row_found OUTPUT

    IF @row_found > 0

    BEGIN

    ---PRINT 'THE TABLE HAS AT LEAST ONE ROW!'

    SET @sqln = 'TRUNCATE TABLE ' + @TableStat

    END --IF

    END --IF

    GO

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Thanks every one especially SSCrazy, that was perfect. Now I can reverse engineer it.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply