Msg 102, Level15, State 1, Line 16 INSERT based on SELECT - sql-server-2008-r2

I have looked at the various other posts about this error message and tried their solutions. Nothing seems to fix my problem. My post pretty much describes what I'm trying to do; I want to create a new shop in my database with the same fields as an existing shop.
This is my code.
INSERT INTO [dbo].[ElementDescriptions] (
[FieldName]
, [DisplayName]
, [Required]
, [DataType]
, [SortOrder]
, [ElementID]
, [Description]
, [Lookup]
, [UIType]
, [RequiredForNew]
, [RequiredForReconditioned]
, [RequredForSecondhand]
, [ShopCode])
SELECT ([FieldName]
, [DisplayName]
, [Required]
, [DataType]
, [SortOrder]
, [ElementID]
, [Description]
, [Lookup]
, [UIType]
, [RequiredForNew]
, [RequiredForReconditioned]
, [RequredForSecondhand]
, 'NEWSHOP')
FROM [dbo].[ElementDescriptions]
WHERE [AARShopCode] = 'OLDSHOP'
My error says it's near ','.

You shouldn't have parentheses around the select list.
INSERT dbo.table(columns) SELECT (columns) FROM ...
Should be:
INSERT dbo.table(columns) SELECT columns FROM ...
---------------------------------^-------^

Related

Parametized Postgres Query with IN clause

I have a query param of type array to collect IDs to query from a postgres table. I think I have built everything out appropriately, but the query fails with ERROR: syntax error at or near "$1"
The logs are:
SELECT
professional_leads.first_name
, professional_leads.last_name
, professional_leads.email
, professional_leads.phone_number
, professional_leads.professional_id as proId
, professional_leads.id as proLeadId
, professional_leads.user_id
, professional_leads.interview_offered_at
, professional_leads.sms_enabled
, professional_leads.email_enabled
, professional_leads.resume_pdf_object_key
, professional_leads.created_at
, professional_leads.updated_at
, professional_leads.reschedule_count
, professional_leads.experience_level
, professional_leads.waitlisted_reason
, professional_leads.resume_state
, professional_leads.interview_state
, professional_leads.state
, professional_leads.profession_id
, professional_leads.indicated_specialty_codes
, professional_leads.other_specialties
, professional_leads.professional_id
, professional_leads.license_received_on
, professional_leads.license_expires_on
, professional_leads.region_id
, professional_leads.marketing_channel
, professional_leads.newsletter
, professional_leads.referral_code
, professional_leads.asset_proof_type
, professional_leads.verification_state
, professional_leads.duplicate
FROM
professional_leads
WHERE
id IN :clause
DEBUG 2022-10-01 21:25:45,346 [[MuleRuntime].uber.15: [api-database-sapi].Copy_of_get-Flow.BLOCKING #7f0aedd] [processor: Copy_of_get-Flow/processors/0/processors/0; event: 232aba80-41f1-11ed-b583-f02f4b10a50d] org.mule.db.commons.shaded.internal.domain.executor.AbstractExecutor: Executing query:
SELECT
professional_leads.first_name
, professional_leads.last_name
, professional_leads.email
, professional_leads.phone_number
, professional_leads.professional_id as proId
, professional_leads.id as proLeadId
, professional_leads.user_id
, professional_leads.interview_offered_at
, professional_leads.sms_enabled
, professional_leads.email_enabled
, professional_leads.resume_pdf_object_key
, professional_leads.created_at
, professional_leads.updated_at
, professional_leads.reschedule_count
, professional_leads.experience_level
, professional_leads.waitlisted_reason
, professional_leads.resume_state
, professional_leads.interview_state
, professional_leads.state
, professional_leads.profession_id
, professional_leads.indicated_specialty_codes
, professional_leads.other_specialties
, professional_leads.professional_id
, professional_leads.license_received_on
, professional_leads.license_expires_on
, professional_leads.region_id
, professional_leads.marketing_channel
, professional_leads.newsletter
, professional_leads.referral_code
, professional_leads.asset_proof_type
, professional_leads.verification_state
, professional_leads.duplicate
FROM
professional_leads
WHERE
id IN ?
Parameters:
clause = ('6a379873-93f9-4b16-8752-168aa92c8846','a234570e-a739-4bcc-847a-a875f5202398')
I flatten the array in a var ids:
"(" ++ (attributes.queryParams.*id map "'$'" joinBy ",") ++ ")"
I have my query as follows:
%dw 2.0
output text
---
"SELECT
professional_leads.first_name
, professional_leads.last_name
, professional_leads.email
, professional_leads.phone_number
, professional_leads.professional_id as proId
, professional_leads.id as proLeadId
, professional_leads.user_id
, professional_leads.interview_offered_at
, professional_leads.sms_enabled
, professional_leads.email_enabled
, professional_leads.resume_pdf_object_key
, professional_leads.created_at
, professional_leads.updated_at
, professional_leads.reschedule_count
, professional_leads.experience_level
, professional_leads.waitlisted_reason
, professional_leads.resume_state
, professional_leads.interview_state
, professional_leads.state
, professional_leads.profession_id
, professional_leads.indicated_specialty_codes
, professional_leads.other_specialties
, professional_leads.professional_id
, professional_leads.license_received_on
, professional_leads.license_expires_on
, professional_leads.region_id
, professional_leads.marketing_channel
, professional_leads.newsletter
, professional_leads.referral_code
, professional_leads.asset_proof_type
, professional_leads.verification_state
, professional_leads.duplicate
FROM
professional_leads
WHERE
id IN :clause"
My input parameters in the call is:
{
"clause": vars.ids
}
Grabbing the query and using the bind variable verbatim, the query executes fine.
Is there a limitation with IN and bind variables?

Creating a table in pdf file using PDF::Report and PDF::Report::Table

In one of my application having a requirement to download a PDF file with report details in form of table.
For creating a PDF file and writing a table in it, using cpan module available in perl. PDF::Report and PDF::Report::Table.
Please find below the code sample:
#!/usr/bin/perl
use strict;
use warnings;
use PDF::Report;
use PDF::Report::Table;
my $pdf = PDF::Report->new( PageSize => 'A4', PageOrientation => 'Portrait' );
my $table = PDF::Report::Table->new( $pdf );
my $data = [
['A1' , 'B1' , 'C1'],
['A2' , 'B2' , 'C2'],
['A3' , 'B3' , 'C3'],
['A4' , 'B4' , 'C4'],
['A5' , 'B5' , 'C5'],
['A6' , 'B6' , 'C6'],
['A7' , 'B7' , 'C7'],
['A8' , 'B8' , 'C8'],
['A9' , 'B9' , 'C9'],
['A10' , 'B10' , 'C10'],
['A11' , 'B11' , 'C11'],
['A12' , 'B12' , 'C12'],
['A13' , 'B13' , 'C13'],
['A14' , 'B14' , 'C14'],
['A15' , 'B15' , 'C15'],
['A16' , 'B16' , 'C16'],
['A17' , 'B17' , 'C17'],
['A18' , 'B18' , 'C18'],
['A19' , 'B19' , 'C19'],
['A20' , 'B20' , 'C20'],
['A21' , 'B21' , 'C21'],
['A22' , 'B22' , 'C22'],
['A23' , 'B23' , 'C23'],
['A24' , 'B24' , 'C24'],
['A25' , 'B25' , 'C25'],
['A26' , 'B26' , 'C26'],
['A27' , 'B27' , 'C27'],
['A28' , 'B28' , 'C28'],
['A29' , 'B29' , 'C29'],
['A30' , 'B30' , 'C30'],
['A31' , 'B31' , 'C31'],
['A32' , 'B32' , 'C32'],
['A33' , 'B33' , 'C33'],
['A34' , 'B34' , 'C34'],
['A35' , 'B35' , 'C35'],
['A36' , 'B36' , 'C36'],
['A37' , 'B37' , 'C37'],
['A38' , 'B38' , 'C38'],
['A39' , 'B39' , 'C39'],
['A40' , 'B40' , 'C40'],
['A41' , 'B41' , 'C41'],
];
$pdf->openpage;
$pdf->setAddTextPos( 50, 50 );
$table->addTable( $data, 400 ); # 400 is table width
$pdf->saveAs( 'table.pdf' );
Result: pdf generated with 2 pages.
at continuity of page missing a row data.
Note: i'm facing issue to attach a span shot of result.
Issues is: missing a row data. missing a row with data [A37, B37, C37].
Please help me in fixing this issues.
Thanks in advance for all your help.
well, when I run your code I get
commandPrompt > ./makepdf.pl
Useless use of greediness modifier '?' in regex; marked by <-- HERE in m/(\S{20}? <-- HERE )(?=\S)/ at /usr/local/share/perl/5.20.2/PDF/Table.pm line 386.
!!! Warning: !!! Incorrect Table Geometry! Setting bottom margin to end of sheet!
at /usr/local/share/perl/5.20.2/PDF/Report/Table.pm line 94.
!!! Warning: !!! Incorrect Table Geometry! Setting bottom margin to end of sheet!
at /usr/local/share/perl/5.20.2/PDF/Report/Table.pm line 94.
I would think that
Setting bottom margin to end of sheet!
and
!!! Warning: !!! Incorrect Table Geometry!
would have something to do with it.

Error : date/time field value out of range: "30/12/2014 23:58:04" postgresql

I am trying to import csv file into postgresql data base
I already tried set datestyle = mdy
\copy "Recon".snapdeal_sales (REFERENCES , ORDER_CODE ,SUB_ORDER_CODE ,
PRODUCT_NAME , ORDER_VERIFIED_DATE , ORDER_CREATED_DATE, AWBNO ,
SHIPPING_PROVIDER , SHIPPING_CITY , SHIPPING_METHOD , INVOICE_NUMBER ,
INVOICE_DATE , IMEI_SERIAL , STATUS , MANIFEST_BY_DATE , SHIPPED_ON ,
DELIVERED_ON , RETURN_INITIATED_ON , RETURN_DELIVERED_ON , SKU_CODE ,
PACKAGE_ID ,PRODUCT_CATEGORY, ATTRIBUTES , IMAGE_URL , PDP_URL , FREEBIES
,TRACKING_URL , ITEM_ID , MANIFEST_CODE , PROMISED_SHIP_DATE ,
NON_SERVICABLE_FROM , HOLD_DATE , HOLD_REASON , MRP
,EXPECTED_DELIVERY_DATE ,TAX_PERCENTAGE , CREATED ,RPI_DATE
,RPI_ISSUE_CATEGORY , RPR_DATE) FROM 'C:\Users\YAM\Documents\SALES.csv' DELIMITER ',' CSV HEADER;
First, run this query.
SET datestyle = dmy;
In my case I was getting this error :
psycopg2.errors.DatetimeFieldOverflow: date/time field value out of range 23-09-2021
soln : check what format the date is present in your db and change the date format in your query accordingly.
In my case it was in a format : yyyy-mm-dd, I was query with dd-mm-yyyy that caused the error, just simply change this.
Ex : http://localhost:port/tweets?date=2021-09-23

Microsoft Report Builder with Postgresql and multiple CTEs

I have a problem creating DataSets with CTE in the current Microsoft Report Designer.
My DataSource is a PostgreSQL Database and I use the current Version of PGNP to connect to.
The connection works fine, I can create DataSets based on Queries and get my data.
BUT if I try to create more complex queries, like for example with multiple CTE, I won't get any fields.
Exampe:
SELECT 'Test' as test
Will return one column (test) with one row (Test)
WITH Test as (SELECT 'Test' as test)
SELECT Test
, 'Success' as result
FROM Test
Will return two columns (test, result) with one row (Test, Success)
Till now, everything is ok.
But if I want to use more then one CTE, I have to face the following problems:
WITH Test as (SELECT 'Test' as test),
TestB as (SELECT 'Test B' as test)
SELECT ta.test, tb.test, 'Success' as result
FROM Test ta, TestB tb
Will fail with the following error message:
"Undefined table Test"
;WITH Test as (SELECT 'Test' as test),
TestB as (SELECT 'Test B' as test)
SELECT ta.test, tb.test, 'Success' as result
FROM Test ta, TestB tb
Will return no columns and no rows.
Any idea why this happens?
Edit A:
Here a (shorternd) Version of my actual query:
WITH calls AS (
SELECT acv.id
, acv.created
, acv.state
, acv.statistics_category
, acv.duration
, aqv.id as queue_id
, aqv.name as queue_name
, acv.target
, acv.target_name
, acv.queue_time
, acv.ringing_duration
, acv.hold_time
FROM acd_call_view acv
LEFT JOIN acd_queue_view aqv ON (aqv.id = acv.fk_acdqueue_id)
ORDER BY acv.id, acv.created
),
calls_answered as (
SELECT *
FROM calls
WHERE statistics_category = 'answered'
)
SELECT * FROM calls_answered
Result: "Unknown table calls"
Edit B:
Sorry, had two leftovers from the shortening remaining in the query
Edit C:
Tested Query in pgAdmin III: Working
Tested Query in JasperSoft Studio: Working
Tested Query in MS Report Builder with PGNP: "Undefined table calls"
Edit D:
As soon as I remove the second CTE, I get results
WITH calls AS (
SELECT acv.id
, acv.created
, acv.state
, acv.statistics_category
, acv.duration
, aqv.id as queue_id
, aqv.name as queue_name
, acv.target
, acv.target_name
, acv.queue_time
, acv.ringing_duration
, acv.hold_time
FROM acd_call_view acv
LEFT JOIN acd_queue_view aqv ON (aqv.id = acv.fk_acdqueue_id)
ORDER BY acv.id, acv.created
)
SELECT * FROM calls
Works fine, so I get results, the PGNP Ole DB Driver seems to work fine, the connection is up, everything is okay.
As soon, as I add the second CTE, I get the error
Works here (just plain psql terminal) , so the error must be in your framework/client application:
CREATE TABLE acd_call_view
( id INTEGER NOT NULL
, created timestamp
, state integer
, statistics_category text
, duration INTEGER
, target INTEGER
, target_name INTEGER
, queue_time INTEGER
, ringing_duration INTEGER
, hold_time INTEGER
, fk_acdqueue_id INTEGER
);
CREATE TABLE acd_queue_view
( id INTEGER NOT NULL
, name text
);
WITH calls AS (
SELECT acv.id
, acv.created
, acv.state
, acv.statistics_category
, acv.duration
, aqv.id as queue_id
, aqv.name as queue_name
, acv.target
, acv.target_name
, acv.queue_time
, acv.ringing_duration
, acv.hold_time
FROM acd_call_view acv
LEFT JOIN acd_queue_view aqv ON aqv.id = acv.fk_acdqueue_id
ORDER BY acv.id, acv.created
)
, calls_answered as (
SELECT *
FROM calls
WHERE statistics_category = 'answered'
)
SELECT * FROM calls_answered
;
Okay, here the solution:
Seems like the PGNP OleDB Driver cannot handle multiple CTEs.
Installed the ODBC Driver and configured a new DateSource via ODBC.
Now I get my results and am happy :-)
Nevertherless, thank you joop for your time.
I contacted PGNP support, and they responded quickly that the bugs in the CTEs handling were fixed in build 1.4.0.3425.

Return substring before and after a certain word

I have a column with large volumes of text in each record. I need to search for a certain word and return the 50 characters before the word and 50 characters after the word. I'm using substring to return the characters after, but can't find a way to return the characters before the word.
substring(RepText,(PATINDEX('certainword',RepText),50) works great for returning text after 'certainword', but not before the word.
Any suggestions would be great!
Here is an example, which prints 2 letters before and after the word sub
declare #st varchar(100) ='test subis good'
select PATINDEX('%sub%', #st)
select SUBSTRING(#st, PATINDEX('%sub%', #st) + LEN('sub'), 2),
SUBSTRING(#st, PATINDEX('%sub%', #st) - ( case when len(#st) < 2 then len(#st) else 2 end), 2)
How about this? If this helps, please up vote my answer so I can get lots of points and look cool :)
if schema_id(N'utility') is null
execute (N'create schema utility');
go
if object_id(N'[utility].[get_some]'
, N'TF') is not null
drop function [utility].[get_some];
go
/*
select [lead], [lag] from [utility].[get_some] (N'return_abcdeandabcde_this', N'and', 5, 5);
select [lead], [lag] from [utility].[get_some] (N'return_a c eanda c e_this', N'and', 5, 5);
select [lead], [lag] from [utility].[get_some] (N'return_ bcd and bcd _this', N'and', 5, 5);
*/
create function [utility].[get_some] (
#input [nvarchar](max)
, #search [nvarchar](max)
, #lead_length [int]
, #lag_length [int])
returns #data table (
[lead] [nvarchar](max)
, [lag] [nvarchar](max))
as
begin
insert into #data
([lead],[lag])
select substring(#input
, charindex(#search
, #input) - #lead_length
, #lead_length) as [lead]
, substring(#input
, charindex(#search, #input) + len(#search)
, #lag_length) as [lag];
return;
end;
go