-1

I have check unique value in the column VariantSKU using this sql code

alter Proc spIsUnique
@columnname nvarchar(max),
@tablename nvarchar(max)
As
Begin
EXEC ('select '+@columnname+',
      IIf (count(*)>1,''False'',''True'') as Total 
      from '+@tablename+' 
      group by '+@columnname)
End

As you can see new column Total which contain True or False.. Now I want to add this column into the table in database. USing function it was not possible so I have created new table exactly same data called "Result" table.How can I add that column Total in Result table. How can I do it?

enter image description here

Awesome
  • 560
  • 2
  • 7
  • 18
  • 2
    Off-topic but worth highlighting; this code is susceptible to [sql injection](https://stackoverflow.com/questions/601300/what-is-sql-injection). – David Rushton Jan 16 '18 at 09:23
  • 1
    alter table...add Total... But what do you mean by "Using Function"? – sepupic Jan 16 '18 at 09:24
  • Please clarify exactly what you are asking – Chris Pickford Jan 16 '18 at 09:29
  • 2
    A `function` can't make DDL changes, or even run dynamic SQL. Also the code you've supplied is for a stored `procedure`, not a `function`. The 2 are very different; what are you really asking here? – Thom A Jan 16 '18 at 09:30
  • Edited.please check – Awesome Jan 16 '18 at 09:33
  • It seems that he wants to add CALCULATED field that equals to True if SKU is unique and False otherwise. He wrote here the code that determines if it's unique or not and wants to use it in a function to use it in calculation for calculated field – sepupic Jan 16 '18 at 09:41
  • 1
    It's usually best *not* to store that which can be calculated from data already stored. Storing the value *introduces* the possibility for the stored value to be *inconsistent* with the base data. – Damien_The_Unbeliever Jan 16 '18 at 09:43
  • I want to store the True/False value in the column and export in csv.That's why I need it – Awesome Jan 16 '18 at 09:52
  • In most circumstances, when exporting to a CSV, you can specify a *query* rather than a *table*. So, again, why would you create a *persistent* copy of this data? – Damien_The_Unbeliever Jan 16 '18 at 17:45

1 Answers1

-1

if i understand well your question then use temp db to stock your table then join it with the destination table or do juste a union

alter Proc spIsUnique
@columnname nvarchar(max),
@tablename nvarchar(max)
As

Begin
   = EXEC ('select '+@columnname+',
      IIf (count(*)>1,''False'',''True'') as Total 
      into ##tempdb
      from '+@tablename+' 
      group by '+@columnname)
  End
select d.* , t.Total from destinationtable as d
inner join ##tempdb as t ON d.@columnname = t.@columnname
ARAZI
  • 126
  • 8
  • This won't work. If you create a temporary table within dynamic SQL, it will only be able to be referenced within that Dynamic SQL Batch. Not outside of it. Try, for example `DECLARE @SQL nvarchar(MAX) = 'SELECT 1 AS i INTO #T;'; EXEC (@SQL); SELECT * FROM #T;` Notice you'll get the error `Invalid object name '#T'.`. – Thom A Jan 16 '18 at 09:44
  • Sorry i mis one # instade of ## he can use ## to declare the table then this query will work i will edite it thanks for the notice – ARAZI Jan 16 '18 at 09:47
  • If you want to use a Global Temporary Table, I'd therefore suggest adding a `DROP` statement at the end too. Otherwise it'll persist in TempDB until the Server restarts. :) – Thom A Jan 16 '18 at 09:52
  • Also, this suffers from the same problem of sql injection that the OP had. – Thom A Jan 16 '18 at 09:56