I need to remove fields from existing table FullNAICS and copy fields from another table NAICS_NEW. FullNAICS having dependencies and unable to drop the table.I need FullNAICS table name and all fields from NAICS_NEW.
I am trying to copy table but getting the error, Please advise.
Query:
INSERT INTO dbo.FullNAICS
SELECT *
FROM dbo.NAICS_NEW
Error:
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.
I have my table design as follows:
NAICS_NEW Table design:
NAICS_US_CODEvarchar(6)
[INDUSTRY TITLE]varchar(100)
NAICS2d varchar(2)
NAICS3d varchar(3)
NAICS4d varchar(4)
NAICS5d varchar(5)
NAICS6d varchar(6)
[Naics2d.txt]varchar(72)
[Naics3d.txt]varchar(87)
[Naics4d.txt]varchar(95)
[Naics5d.txt]varchar(97)
[Naics6d.txt]varchar(100)
FullNAICS Table design:
NAICSCd6d nvarchar(255) Unchecked
NAICA6dTxt nvarchar(255) Checked
NAICSCd5d nvarchar(255) Checked
NAICS5dTxt nvarchar(255) Checked
NAICSCd4d nvarchar(255) Checked
NAICS4dTxt nvarchar(255) Checked
NAICSCd3d nvarchar(255) Checked
NAICS3dTxt nvarchar(255) Checked
NAICSCd2d nvarchar(255) Checked
NAICS2dTxt nvarchar(255) Checked
[NAICS US CODE] nchar(10) Checked
[INDUSTRY TITLE] nchar(10) Checked
Unchecked
Your tables are using varchar and nvarchar,nchar
if you want to suppress the message use CAST and CONVERT
for all columns
SELECT
CAST(NAICS_US_CODE as nvarchar) ,
CAST([INDUSTRY TITLE] as nvarchar), CAST ....
FROM dbo.NAICS_NEW
CAST and CONVERT (Transact-SQL)
https://msdn.microsoft.com/en-ca/library/ms187928.aspx
Related
I need a SQL guru, this is driving me crazy...
I'm getting an error when executing dynamic SQL with a user-defined Table Type:
Error: Operand type clash: VariableTableType is incompatible with VariableTableType
Problem: It doesn’t seem to really declare the VariableTableType in the [Database1] database even though the Use [Database1] is right above it.
The XT function has to run in the [Database1] database for it to work.
If I run the exec with the dbname, it works- ex: exec database1.dbo.sp_executesql #cmdTest – but I have many client databases that have the same database structure (different data) where I have to pass in the client database name so I can’t hardcode it that way.
If I make #dbname=’MainDatabase’ it will run – just doesn’t run against the right database.
[MainDatabase]
Contains main stored proc that has the dynamic SQL:
#dbname=’Database1’
DECLARE #cmdTEST NVARCHAR(MAX)=N'USE '+#dbname +'
DECLARE #t as dbo.VariableTableType
INSERT #t
SELECT VariableName,[Value],InstanceID FROM #tVarTble
SELECT dbo.XT(''Test string'',7153,45,#t)RtnValue
'
exec sp_executesql #cmdTEST
Also tried passing in the VariableTableType and put the declaration outside of the dynamic sql.
--exec sp_executesql #cmdTEST,N'#t VariableTableType READONLY',t=#t
[Database1] (this function also exists in the [MainDatabase]
ALTER FUNCTION [dbo].[XT]
(
#text nvarchar(max),
#instanceID INT,
#storeID INT,
#vt dbo.VariableTableType READONLY
)
RETURNS nvarchar(max)
AS BEGIN
… code here
RETURN #return
END
VariableTableType exists in the [MainDatabase] and [Database1]
CREATE TYPE [dbo].[VariableTableType] AS TABLE(
[VariableName] [NVARCHAR](50) NULL,
[Value] [NVARCHAR](50) NULL,
[InstanceID] [INT] NULL
)
I am trying to convert code given by teacher that is in SQL Server to MySQL. It ultimately should allow me to input information to the table that was made. This is a for a guest book assignment. Unfortunately I cannot find the correct syntax as I have never worked with SQL programming in general before.
I have tried using brackets,semi colons and commas for the information as well as adding a delimeter which eliminated the "Unrecognized statement type (near PROCEDURE)" error messages that had initially been in the programming as well.
Teachers code:
CREATE PROCEDURE spInsertGuestbookEntry
#GuestBookName varchar(200),
#GuestBookEntry ntext,
#GuestbookEmail varchar(200),
#GuestBookIP varchar(20)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Guestbook
(GuestbookName, GuestBookEntry, GuestbookEmail, GuestBookIP, GuestBookDate)
VALUES
(#GuestbookName, #GuestBookEntry, #GuestbookEmail, #GuestBookIP, GetDate())
END
GO
My modifications:
DELIMITER $$
CREATE PROCEDURE spInsertGuestbookEntry
#GuestBookName varchar(200)
#GuestBookEntry text
#GuestbookEmail varchar(200)
#GuestBookIP varchar(20)
#GuestBookDate date
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Guestbook
(GuestbookName, GuestBookEntry, GuestbookEmail, GuestBookIP, GuestBookDate)
VALUES
(#GuestbookName, #GuestBookEntry, #GuestbookEmail, #GuestBookIP, #GuestBookDate)
END
GO
The error messages that I got was "Unrecognized data type. (near))" That error message is by the #GuestBookName varchar(200)
DELIMITER $$
CREATE PROCEDURE `spInsertGuestbookEntry`(
IN `str_GuestBookName` VARCHAR(200),
IN `str_GuestBookEntry` TEXT,
IN `str_GuestbookEmail` VARCHAR(200),
IN `str_GuestBookIP` VARCHAR(20)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
INSERT INTO Guestbook
(GuestbookName, GuestBookEntry, GuestbookEmail, GuestBookIP, GuestBookDate)
VALUES
(str_GuestBookName, str_GuestBookEntry, str_GuestbookEmail, str_GuestBookIP, CURRENT_DATE());
END
$$
Key points to consider:
Distinguish between table column_name and procedure input parameter.
# is used to create user-defined variables in MySQL and you should be using this if your parameter is of type OUT/INOUT while calling procedure.
In procedure/function creation, simple variable names are used, not with # symbol otherwise while using these parameters, MySQL will look for some variable with this name, not the input parameter.
I have seen few post about using a variable name with a bulk insert but I cannot make it work.
I have to do 20 bulk inserts in the same scripts, the directory is the same but the filename changes.I cannot get the syntax right.
this is what I have done
DECLARE #CsvFilePath varchar(255)
DECLARE #SQL_BULK VARCHAR(MAX)
SET #CsvFilePath='C:\MyDirectory'
IF object_id(N'MyTable', 'U') IS NOT NULL
DROP TABLE MyTable
GO
CREATE TABLE MyTable
(
CustomerNo
)
SET #SQL_BULK='BULK INSERT MyTable FROM ''' + #CsvFilePath + '\MyFileName.txt' + '''
WITH (FIRSTROW=2, FIELDTERMINATOR='~',ROWTERMINATOR='\n')'
exec (#SQL_BULK)
GO
Any suggestions?
my problem is While passing 2 parameters month and year of varchar datatype,i need to club two by '/' and i need to compare with a coloumn(month/year) and then have to show in SQL server. can any one please help me.. thanks in advance
Here is an example table I created :
Create table SampleTable (Id numeric(10,0) identity(1,1) primary key, MonthAndYear varchar(7));
Insert into SampleTable values('01/14');
Insert into SampleTable values('02/14');
And the stored procedure :
Create procedure compareMonthAndYear
#month varchar(2),
#year varchar(2)
AS
BEGIN
SET NOCOUNT ON;
Select * from SampleTable s
where s.MonthAndYear like (#month + '/' + #year)
END
Run the stored procedure like :
compareMonthAndYear '02','14'
Hope you could get an idea!
I want to give an array of identifiers as argument to a stored procedure.
The stored procedure looks like :
ALTER PROCEDURE [dbo].[SearchPerson]
#personType INT = NULL,
#city NVARCHAR(64) = NULL,
#siteIds IntegerList READONLY,
-- some other params...
AS
SELECT
-- some fields...
FROM dbo.PersonView AS pv
WHERE
(
(#personType IS NULL OR pv.PersonType = #personType) AND
(#city IS NULL OR pv.City LIKE '%' + #city + '%') AND
(pv.SiteId in (SELECT si.Value FROM #siteIds AS si)) AND
-- some other params filter...
)
The user table type looks like :
CREATE TYPE [dbo].[IntegerList] AS TABLE(
[Value] [int] NULL
)
When I call the stored procedure from a script in SSMS (I originally have the same problem calling it from .NET code) :
DECLARE #siteIds AS IntegerList,
#personType AS INT = 1
INSERT INTO #siteIds VALUES (1)
EXEC [dbo].[SearchPerson] #personType, #siteIds
I got the error :
Operand type clash: int is incompatible with IntegerList
I found the answer : it was the order of the table type parameter that caused the error !
The table type parameter must be the first in the stored procedure parameters AND ALSO in the arguments passed to the stored procedure call !
The stored procedure :
ALTER PROCEDURE [dbo].[SearchPerson]
#siteIds IntegerList READONLY, -- THIS PARAMETER HAS TO BE THE FIRST !
#personType INT = NULL,
#city NVARCHAR(64) = NULL,
-- some other params...
AS
SELECT
-- some fields...
FROM dbo.PersonView AS pv
WHERE
(
(#personType IS NULL OR pv.PersonType = #personType) AND
(#city IS NULL OR pv.City LIKE '%' + #city + '%') AND
(pv.SiteId in (SELECT si.Value FROM #siteIds AS si)) AND
-- some other params filter...
)
And the call :
DECLARE #siteIds AS IntegerList,
#personType AS INT = 1
INSERT INTO #siteIds VALUES (1)
EXEC [dbo].[SearchPerson] #siteIds, #personType -- PUT #siteIds FIRST !
A sql server bug or am I missing something ?
DECLARE #ErrMsg varchar(1000)
DECLARE #ServiceDates ServiceDatesType
INSERT #ServiceDates (indexId,unitOfDay,dayOfMonth,dateOfMonth)
VALUES
(0,1,11,'9/11/2016 12:00:00 AM'),
(1,1,12,'9/12/2016 12:00:00 AM'),
(2,1,13,'9/13/2016 12:00:00 AM')
EXEC [usp_SaveValidate] 427,4,12,9,2016,#ErrMsg output,#ServiceDates
PRINT #ErrMsg
*/
ALTER PROCEDURE [dbo].[usp_SaveValidate] (
#EpisodeNo INT
,#ProviderId INT
,#ServiceId INT
,#Month INT
,#Year INT
,#ErrorMessage VARCHAR(1000) OUTPUT
,#ServiceDates ServiceDatesType ReadOnly
)
AS
BEGIN
-- Code Here
END
SQL SERVER 2012 - location of table type parameter does not matter, you have to make sure sequence while passing data, you can check above code which is working fine where table type parameter is at the last.