INSERT INTO #Prefix_PCAC_temp select rtrim(ltrim(replace(#str1,"'",null)))
in the above line it is showing error invalid column name '''.PLZ provide me any solution.
Use this to prevent incorrect syntax error:
INSERT INTO #Prefix_PCAC_temp select rtrim(ltrim(replace(#str1,'''',null)))
However, if there is ' symbol in your #str1 variable, the value will become NULL. Most likely you want this:
INSERT INTO #Prefix_PCAC_temp select rtrim(ltrim(replace(#str1,'''','')))
The query above eliminates ' symbols from #str1 variable and inserts the value into the table
Related
I have the following code snippet from a SQL Server Stored Procedure.
DECLARE #sql nvarchar(Max)
SET #sql = N'
WITH CTE AS
(
SELECT * FROM #TEMP WHERE ...
),
Track AS
(
SELECT *,
CASE WHEN 1B1 = "Track" AND (QTRK_1B1 = "2D QT" OR QTRK_1B1 = "3D QT") THEN "Yes" ELSE "No" END AS Tracking
FROM CTE
),
... '
Exec sp_executesql #sql, N'#AirTarget nvarchar(25), #GroundTracker nvarchar(25)', #AirTarget, #GroundTracker
I am not sure what I am doing wrong here. When I execute the stored procedure, I am getting errors like:
Invalid Column Name 'Track'
Invalid Column Name '2D QT'
Invalid Column Name '3D QT'
Invalid Column Name 'Yes'
Invalid Column Name 'No'
But those are not column names from the table. I have Googled and cannot figure out what I am doing wrong.
I have tried surrounding those values with single quotes, and I have tried removing the quotes from around those values and nothing works. Any help would be appreciated
SQL use single quote ' for string
CASE
WHEN 1B1 = ''Track'' AND (QTRK_1B1 = ''2D QT'' OR QTRK_1B1 = ''3D QT'') THEN ''Yes''
ELSE ''No''
END AS Tracking
They all definitely should be single quotes - all places where you have double-quotes. If
you do that it should at least give you a different message.
You'll also need to wrap 1B1 in square brackets like [1B1] as it won't like a column name starting with a number in a CASE statement.
I am trying to convert a column formatted in varchar to decimal(19,12) with the following line of code
ALTER TABLE [tablename]
ALTER COLUMN [columnname][format]
and get the following prompt:
Msg 8114, Level 16, State 5, Line 25
Error converting data type varchar to numeric.
Has worked before like a charm. The issue here seems to be that the values in the column are 19 or so digit numeric values formatted as text.
I tried to create a new column, pasted shortened cell values (used the left() function) into it from the original column but that doesn't seem to do the trick either since the code above ends up occationally with the additional "Arithmetic overflow occurred." message.
When some of the rows have incorrect values, ALTER COLUMN would not work. A typical course of action goes like this:
Add a new column of the desired type
Update the column with values that you would like to keep
Drop the old column
Rename the new column
Step 2 would go like this:
UPDATE MyTable
SET NewColumn =
CASE WHEN ISNUMERIC(OldColumn)=1 AND DATALENGTH(OldColumn) <= 19 THEN
CAST(OldColumn AS decimal(19,12))
ELSE
NULL
END
You could also turn ANSI warnings off with SET ANSI_WARNINGS OFF command, which would let you run ALTER COLUMN ignoring data trunction errors. The drawback of this approach is that potential errors get ignored. On the other hand, when you do conversion explicitly with a CASE expression you have an option to supply an alternative value for the error case (I used NULL above, but you can put any number you want).
Could you try to seperate your problem? This does work on SQL 2012:
set nocount on
if object_id ('tempdb..#t1') is not null drop table #t1
create table #t1 (c1 varchar(100))
insert #t1 values ('1234567.8901234567890')
select * from #t1
alter table #t1
alter column c1 decimal(19,12)
select * from #t1
If you play around a bit with the strings you easily can produce an arimetic overflow error. But 'Error converting data type varchar to numeric' needs character or empty sting.
Maybe you can try with your data?
I'm trying to create a function that takes the parameters for the column, the table, the limit, and offset. Basically, I want to be able to get a specified number of rows data from a specified table from a specified column.
However, I'm unable to get the following code to work - I get several errors such as:
syntax error, unexpected SELECT, expecting ':' in: "create function get_banana(lim int, off int, tbl varchar(32), col varchar(32)) r"
syntax error, unexpected RETURN in: "return"
syntax error, unexpected END in: "end"
These errors seem kind of meaningless.
My code is as follows:
CREATE FUNCTION GET_BANANA(lim int, off int, tbl varchar(32), col varchar(32))
RETURNS TABLE (clm int)
BEGIN
PREPARE SELECT col FROM tbl LIMIT ? OFFSET ?;
RETURN EXEC (lim, off);
END;
I'd appreciate any help :) Thanks!
I see at least two issues
EXEC needs the identifier that is returned by PREPARE, e.g.:
sql>prepare select * from tables;
execute prepared statement using: EXEC 2(...)
sql>exec 2();
The function parameters tbl and col are string values. You cannot use them as table/column identifiers.
Having said that, I am not even sure if PREPARE can be used inside a function.
No, PREPARE is a top-level statement modifier.
I have a table in my SQL Server database with a column of type nvarchar(50);
I tried to do this:
Right click on my table
Selecting "Edit 200 first rows"
Typing in that column 'a'
I get an error like this:
Error source: .Net SqlClient Data Provider.
Error message: Incorrect syntax near 'a'.
Correct the errors and retry".
Why do I get this error message and how can I fix this?
I have used aqua data studio and tested this it is allowing me.
my result:::
id testc
----- ---------
1 'asdfasd'
If you want to use it thrugh .net then while passing the string add the escape sequences. suppose if you are sending "'"--single quote.. then send I have used and tested this it like "\'" is allowing me.
refer below link:::
http://www.codeproject.com/Questions/157918/How-can-I-insert-a-special-character-in-sql-server
I have had the same error.
I want to insert a value I am Nam 'ABC' into MyTable at field Description as following SQL statement
Insert Into table MyTable (Description) values('I am Nam 'ABC'');
Once Insert command executed the error occur immediately
Incorrect syntax near 'ABC'.
Note that the reason why is SQL will understand character " ' " which before ABC belongs to character
" ' " before I, and one after ABC belongs to " ' " at the end. As a result ABC does not belong to SQL
statement anymore therefore it makes a break of statement.
Here is my solution:
I added two single quotes for each side of ABC
Insert Into table MyTable (Description) values('I am Nam ' 'ABC' ' ');
I'm using Postgresql and PHP 5.3.x with PDO to access the DB.
I have this the SQL query (stripped down version), with a placeholder for PDO to fill in:
INSERT INTO t_articles (a_article_id) VALUES (?) RETURNING a_id
I want a_article_id to be either a number, like 5, or else it should be the result of the subquery:
((SELECT max(a_article_id) FROM t_articles) + 1)
However, PDO says:
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "(SELECT max(a_article_id) FROM t_articles) + 1"
And I've tried to set the subquery as the default value, but it is not allowed apparently:
ERROR: cannot use sub query in default expression
How can I insert the result of this sub query (or what can be done to achieve the same result)?
You'd have to use INSERT...SELECT for that:
insert into t_articles (a_article_id)
select max(a_article_id) + 1
from t_articles
returning id
Or if you don't need contiguous values for a_article_id, use a sequence for it:
Create a sequence, we'll call it article_id_sequence.
-- Get the current max(a_article_id)+1 to use instead of FIRST_VAL below
create sequence article_id_sequence
start FIRST_VAL
owned by t_articles.a_article_id;
Set the default value for t_articles.a_article_id to nextval('article_id_sequence').
alter table t_articles
alter column a_article_id
set default nextval('article_id_sequence');
Use the default value when inserting:
insert into t_articles (a_article_id)
values (default)
returning id;