BCP use produces incorrect syntax error - sql-server

I am new to the bcp tool. Below is sample code copied from the Microsoft site which I adapted for our server. This is for SQL server.
It works fine until the last line, which is the bcp call.
Here it get the error message
"Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '-'."
Would this be some kind of issue where bcp is not installed on our server?
Thanks
USE CH_Reports;
GO
if OBJECT_ID('myTestCharData','Table') is not null
drop table myTestCharData
CREATE TABLE myTestCharData (
Col1 smallint,
Col2 nvarchar(50),
Col3 nvarchar(50)
);
INSERT INTO myTestCharData(Col1,Col2,Col3)
VALUES(1,'DataField2','DataField3');
INSERT INTO myTestCharData(Col1,Col2,Col3)
VALUES(2,'DataField2','DataField3');
GO
SELECT Col1,Col2,Col3 FROM myTestCharData
-bcp myTestCharData out C:\myTestCharData-c.Dat -c -t, -T

Related

how to use a passed variable from sqlcmd using -v in if statement of sql script

I am working with Microsoft SQL Server 2016 server. Assume the server's name is ServerA and the database's name is DatabaseA.
I want to write a sql script which will accept a parameter. Based on that parameter's value the sql script may execute different SQL statements.
I will be calling the sql script from sqlcmd passing the parameter using -v option.
So I wrote my SQL script ProjectType.sql. The parameter name is ProjectType. This is a basic idea of the SQL:
if $(ProjectType) = 'Sales'
begin
-- my 1st set of sql statements
select count(*) 'person_count' from person;
end
else if $(ProjectType) = 'Support'
begin
-- my 2nd set of sql statements
select count(*) 'employee_count' from employee;
end
Then I try to run using sqlcmd
sqlcmd -S ServerA -d DatabaseA -v ProjectType="Sales" -i ProjectType.sql
But I got error message:
Msg 156, Level 15, State 1, Server ServerA, Line 1
Invalid column name 'Sales'.
Msg 102, Level 15, State 1, Server ServerA, Line 6
Invalid column name 'Sales'.
How can I resolve this issue?

How to use openquery for inserting geometry data from my server into linked server

I am trying to use a query modified from MS docs to insert GEOMETRY data from my sql server into a linked sql server (Azure).
openquery for select works fine with the client's choice of naming for their db which includes '-' ([their-db-name]).
SELECT * FROM openquery([LINKED SERVER],
'SELECT [geometry] FROM [their-db-name].[theirSchema].[Dimensions]');
** correct result set returned **
However when I try INSERT I get:
INSERT openquery([LINKED SERVER],
'SELECT [geometry] FROM [their-db-name].[theirSchema].[Dimensions]')
SELECT [geometry] FROM [my_db].[dbo].[Dimensions];
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near '-'.
I don't have any control over their use of '-' in naming, however since the name is enclosed in '[]', and the openquery SELECT statement works fine I have exhausted all the syntax errors I can think of.
Using 'INSERT INTO' makes no difference.
How can I make an insert work?
Thanks in advance!

Insert stored procedure data in a temp table in SQL Server

I am trying to insert the data of a stored procedure into a temp table like below
CREATE TABLE #CustomTable3HTML
(
ItemId varchar(30),
ItemId1 varchar(30)
)
INSERT INTO #CustomTable3HTML
EXEC SalesDeals.dbo.prGetDealProposalDetail 17100102, 1
but I am getting this error
Msg 8164, Level 16, State 1, Procedure prGetDealProposalDetail, Line 138 [Batch Start Line 1]
An INSERT EXEC statement cannot be nested.
I figured this is because the stored procedure already has an insert into clause defined and I found out that it can be used only once in the calling chain.
So I started looking for other options and found out about OpenRowSet which I am using as below
SELECT *
INTO #CustomTable3HTML
FROM OPENROWSET('SQLOLEDB','Server=Demo\Demo;Trusted_Connection=Yes;Database=SalesDeals',
'SET NOCOUNT ON;SET FMTONLY OFF;EXEC SalesDeals.dbo.prGetDealProposalDetail 17100102,1')
I am getting an error when I run this SQL command
Access to the remote server is denied because no login-mapping exists.
It works fine when I use a higher level account like sysadmin but fails with the other account which is a normal db owner on the database where I am running this SQL.
There is work around of this. It's not beautiful, but it will work.
In our outer query define a table:
CREATE TABLE #CustomTable3HTML
(
ItemId varchar(30),
ItemId1 varchar(30)
)
Change the procedure adding the following code at the end:
IF OBJECT_ID('tempdb..#CustomTable3HTML')
BEGIN
INSERT INTO #CustomTable3HTML
SELECT ....
END
ELSE
BEGIN
SELECT ....
END
After executing the stored procedure you will have the data in table.

TSQL insert exec(#command)

I am wondering why below statement is not working:
insert into #temp (ip, ping) values ('ip', exec xp_cmdshell 'ping ip')
I would like to get resultset where I will have ip address in one column and ping from that server. Above query returns errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'exec'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
Thanks in advance for explanations.
You can't execute anything in a INSERT statement value list. You need to execute like this:
insert into #temp
execute xp_cmdshell 'ping ip'
[This assumes that the #temp table has a column structure matching the resultset of xp_cmdshell ]
You can also create an intermediate temp table to contain all the columns that the stored procedure returns and then insert just the ones you want into your final table.
Well, insert ... exec is notoriously inflexible. For one thing, it won't work with a values clause. Even more annoyingly, it does not support a column list. The columns in the table have to match the output of the stored procedure exactly.
The only way to use insert ... exec is:
insert TableName exec(...)
-- ^^^--> no column list!
Here's an example:
if exists (select * from tempdb.sys.tables where name like '#temp__%')
drop table #temp
create table #temp (output varchar(max))
insert into #temp execute xp_cmdshell 'ping pong'
select * from #temp

Inserting multiple values into a temporary table, SQL Server

I am using Microsoft SQL Server Management Studio, I am trying to run the following query to input values into a temporary table to use later:
CREATE TABLE #temptable
(colnumber varchar(15), dispcode varchar(10))
INSERT INTO #temptable (colnumber, dispcode)
VALUES
('col5', '811'),
('col6', '817'),
('col7', '823'),
('col8', '825');
When running I get the following error:
Msg 102, Level 15, State 1, Line 50
Incorrect syntax near ','.
Which points to the line "('col5', '811'),"
Could anyone help me identify the problem here?
For SQL Server version <2008 use this:
INSERT INTO #temptable (colnumber, dispcode)
SELECT 'col5', '811'
UNION ALL SELECT 'col6', '817'
UNION ALL SELECT 'col7', '823'
UNION ALL SELECT 'col8', '825'

Resources