Conversion error with simple SQL UPDATE statement - sql-server

I have an application that is using SQL Server 2016 for backend storage. I have the following statement I'm trying to run:
UPDATE dbo.AspNetUsers
SET [IsActive] = 1
WHERE [Id] = '1b08b7a9-2978-4116-8a9c-e86bc9ae8bbf'
[IsActive] is a BIT column
[Id] is a NVARCHAR(128) column
When executing, I get the following error:
Msg 245, Level 16, State 1, Procedure tUserActiveChange, Line 21 [Batch Start Line 4]
Conversion failed when converting the nvarchar value '1b08b7a9-2978-4116-8a9c-e86bc9ae8bbf' to data type int.
Seems like a pretty straightforward update statement. I even tried the same type of statement on another database on the same server where the updated column is a BIT and using a VARCHAR column for the WHERE condition and it worked just fine.
What the heck?

Well, I realized I had a trigger I created a while back that is causing the error (tUserActiveChange). The trigger updates another table when IsActive changes. It was, in fact, trying to assign a VARCHAR variable to an int field.
I fixed that and now the UPDATE statement is working fine. Thank you all for taking the time to look into this. I feel silly now haha.

Related

Can create a table but doesn't display in Object explorer, can't select, or delete table?

Using SQL Server 2016. Have a locally hosted database that uses the Windows login for the sa, which is what I am using to login.
Yesterday I ran
CREATE TABLE [Otis].[AnalyzerGroups]
(
[id] [int] IDENTITY,
[Name] [varchar](50) NULL
);
and got command successfully completed. Today I tried selected from this table but got an error:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'Otis.AnalyzerGroups'
So I thought I misremembered and tried running the create statement again but then got the error -
Msg 15530, Level 16, State 1, Line 1
The object with name "AnalyzerGroups" already exists.
The statement has been terminated.
So then I tried DROP TABLE [Otis].[AnalyzerGroups]; and got
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'Otis.AnalyzerGroups', because it does not exist or you do not have permission.
I tried making a new test table and the same thing. The first time I run the create statement command successfully completes, but then I can't select / insert / drop from the table, and I cannot see it in the Object explorer either.
I assume this must be some permissions issue but I don't know what property is keeping me from viewing these tables - its not like I'm putting security on these tables, and I can see every other table in our database. Any thoughts?
You put it most likely in the wrong database. Try this.
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like
''%AnalyzerGroups%'''
There was a trigger in the SQL Server database that fired any time a new table was created under any schema, and would then transfer it to the dbo schema. So my tables did exist, but they were under the schema I was expecting because of this trigger. Why does this trigger exist? Got me. But it's in production and has been for over a decade so there's some reason/it's definitely not changing. Thanks for the help though everyone.

SQL DML - unable to use the after function in modify

I need to update around 1000 xml records with a new xml node, the xml's are stored in a sql table column, with xml datatype, So I've been trying to utilize the modify function documented HERE (specifically example A.4)
However, When I run this against my staging environment (SqlServer 2016 SP2-CU13) I receive the following error message:
Msg 2395, Level 16, State 1, Line 7
XQuery [production.dbo.organisations.Configuration.modify()]: There is no function '{http://www.w3.org/2004/07/xpath-functions}:AFTER()'
The code is rudimentary, I'm just stumped by the error message. Any help or insight is greatly appreciated
Code:
update [production].[dbo].[organisations] Set configuration.modify('INSERT <LegalEntityName>TEST</LegalEntityName> AFTER (/OrganisationMetaData/ExamPrepAssessModeFromAddress)[1]') where ID = 1
Remember XML is always case-sensitive, eg:
use tempdb
go
drop table if exists organisations
go
create table organisations(id int, configuration xml)
insert into organisations(id, configuration) values (1,'<OrganisationMetaData><ExamPrepAssessModeFromAddress></ExamPrepAssessModeFromAddress></OrganisationMetaData>')
update organisations Set configuration.modify('insert <LegalEntityName>TEST</LegalEntityName> after (/OrganisationMetaData/ExamPrepAssessModeFromAddress)[1]') where ID = 1

SQL Server: Error converting data type varchar to numeric (Strange Behaviour)

I'm working on a legacy system using SQL Server in 2000 compatibility mode. There's a stored procedure that selects from a query into a virtual table.
When I run the query, I get the following error:
Error converting data type varchar to numeric
which initially tells me that something stringy is trying to make its way into a numeric column.
To debug, I created the virtual table as a physical table and started eliminating each column.
The culprit column is called accnum (which stores a bank account number, which has a source data type of varchar(21)), which I'm trying to insert into a numeric(16,0) column, which obviously could cause issues.
So I made the accnum column varchar(21) as well in the physical table I created and it imports 100%. I also added an additional column called accnum2 and made it numeric(16,0).
After the data is imported, I proceeded to update accnum2 to the value of accnum. Lo and behold, it updates without an error, yet it wouldn't work with an insert into...select query.
I have to work with the data types provided. Any ideas how I can get around this?
Can you try to use conversion in your insert statement like this:
SELECT [accnum] = CASE ISNUMERIC(accnum)
WHEN 0 THEN NULL
ELSE CAST(accnum AS NUMERIC(16, 0))
END

The conversion of a nvarchar data type to a datetime

I have execute new query as below:
alter table tenancy_extend_old alter column createdate datetime
but it show messages error as below:
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Please help me. thanks
I suggest you:
-Create a new column called something like CD of type datetime
-Update the data accross using a suitable format. If your data is consistently in format Dec 18 2001 (which it probably isn't assuming the old column is varchar) then you can use this:
UPDATE tenancy_extend_old
SET CD = CONVERT(DATETIME,createdate ,100)
It's likely you'll get the same error because someone chose the incorrect data type originally, so you'll need to use Aarons suggestion to find bad data
Once you've done that, drop the old column and rename the new column

SQL Server 2005 Alter table column not being recognized

This is on a replicated table in SQL Server 2005
I used the following command:
ALter table dbo.apds alter column docket nvarchar(12) null
and it executed with no errors, everything looks clean.
Column spec shows it now has 12 (was previously set to 6) on both tables, publisher
and subscriber.
But when I try to put more than 6 characters in that column, I get the error:
Msg 8152 lefel 16, state 13, procedure trgapdsupdate, line 5
String or binary data would be truncated.
I can still only write 6 characters of data to that column even though it shows 12 as the
column specification..
Any ideas?
Thank you in advance..
You say
I get the error: msg 8152 lefel 16, state 13, procedure trgapdsupdate,
line 5 string or binary data would be truncated. The statement has
been terminated
So what is trgapdsupdate?
From the name it looks like an update trigger on the table apds?
Does that need to be updated to deal with the new column value? For example writing to an audit table whose definition needs to be updated.

Resources