What SQL argument have I got wrong? - sql

Background: I have both SQL2008 and SQL2012 installed on my machine. This has resulted in only having the studio management for 2012 running, and any amount of trying wont let me add the studio for 2008.
Problem: Now I have backup that for verification reasons need to be restored on the 2008 instance without the 2012 management studio to avoid contamination, so I am trying to run the restore directly from the command prompt. I have let the 2012 studio make the actual command for me, and am now trying to put that into the prompt, but it won't run; and no amount of reading makes me see the invalid argument. I realize I am probably missing something elemental, but please have a look at it anyway.
The command that I am trying to run is:
C:\Program Files\Microsoft SQL Server\100\Tools\Binn>SqlCmd -E -S SQL2008 "RESTORE DATABASE Basename FROM DISK = N'D:\Folder\Backup.BAK' WITH FILE = 1, MOVE N'Base_Data' TO N'C:\Folder\Base.mdf', MOVE N'Base_1_Data' TO N'C:\Folder\Base_1.ndf', MOVE N'Base_2_Data' TO N'C:\Folder\Base_2.ndf', MOVE N'Base_Log' TO N'C:\Folder\Base_3.LDF', NOUNLOAD, STATS = 5"
And the response is a quick: Unexpected argument. Enter '-?' for help.
I don't see how -E or -S could be wrong, and the server is correct 2008 instance. I have tried it without the MOVEs to see if there could be something there, but it gave the same result.
So could someone please swing a 2 by 4 my way letting me know what I am doing wrong? Thank you

Maybe like this?
C:\Program Files\Microsoft SQL Server\100\Tools\Binn>SqlCmd -E -S SQL2008 -Q "RESTORE DATABASE Basename FROM DISK = N'D:\Folder\Backup.BAK' WITH FILE = 1, MOVE N'Base_Data' TO N'C:\Folder\Base.mdf', MOVE N'Base_1_Data' TO N'C:\Folder\Base_1.ndf', MOVE N'Base_2_Data' TO N'C:\Folder\Base_2.ndf', MOVE N'Base_Log' TO N'C:\Folder\Base_3.LDF', NOUNLOAD, STATS = 5"

It seems like the most common issue with this problem is copying and pasting from some other source, as mentioned in the comments in the other answer. My problem was copying and pasting from this article:
http://www.howtogeek.com/50295/backup-your-sql-server-database-from-the-command-line/
To fix this, try typing your command in manually so you know the exact value of every character. Usually the problem characters are double quotes, but they can also be single quotes, and my problem was in fact the dash character being U-2014, the "EN DASH". Even the spaces could potentially be nonblocking spaces i suppose.

Related

SQL16010: Incorrect syntaxt after using :r on a database project

I have a database project with the following structure
When I try to publish the profile, the VS compiles de code before and is showing me the following error:
SQL46010: Incorrect syntax near .
I have this option enable for the SQLCMD on my VS configurations
My OneTimeMaster.sql looks also has another error after :r, the code looks like this
:setvar path ".\Sprint 1.11"
:r $(path)\Header.sql
How can I make it run just to get the generated script.
I am assuming that your post-deployment or pre-depolyment script are pointing to your OneTimeMaster.sql, on that case if you have your SQLCMD activated, the pre and post deployment sqls will not have any error, but when you try to make a build seems like the other files interpreted by the compiler as regular sql without the SQLCMD command. I tested your scenario and the way that I was able to generate the script was changing the Property Action of the OneTimeMaster.sql and all the subsql files to None. Doing that the generated script had the merge of your Testing1.sql and Testing2.sql. Hope this helps

How to split sql in MAC OSX?

Is there any app for mac to split sql files or even script?
I have a large files which i have to upload it to hosting that doesn't support files over 8 MB.
*I don't have SSH access
You can use this : http://www.ozerov.de/bigdump/
Or
Use this command to split the sql file
split -l 5000 ./path/to/mysqldump.sql ./mysqldump/dbpart-
The split command takes a file and breaks it into multiple files. The -l 5000 part tells it to split the file every five thousand lines. The next bit is the path to your file, and the next part is the path you want to save the output to. Files will be saved as whatever filename you specify (e.g. “dbpart-”) with an alphabetical letter combination appended.
Now you should be able to import your files one at a time through phpMyAdmin without issue.
More info http://www.webmaster-source.com/2011/09/26/how-to-import-a-very-large-sql-dump-with-phpmyadmin/
This tool should do the trick: MySQLDumpSplitter
It's free and open source.
Unlike the accepted answer to this question, this app will always keep extended inserts intact so the precise form of your query doesn't matter; the resulting files will always have valid SQL syntax.
Full disclosure: I am a share holder of the company that hosts this program.
The UploadDir feature in phpMyAdmin could help you, if you have FTP access and can modify your phpMyAdmin's configuration (or are allowed to install your own instance of phpMyAdmin).
http://docs.phpmyadmin.net/en/latest/config.html?highlight=uploaddir#cfg_UploadDir
You can split into working SQL statements with:
csplit -s -f db-part db.sql "/^# Dump of table/" "{99}"
Which makes up to 99 files named 'db-part[n]' from db.sql
You can use "CREATE TABLE" or "INSERT INTO" instead of "# Dump of ..."
Also: Avoid installing any programs or uploading your data into any online service. You don't know what will be done with your information!

ERROR attaching adventureworks2012_data.mdf file with sql server 2012

I have been trying to add adventure works database to my SQL server 2012..I tried to attach the database using SQL Server Management Studio as follows: I Right Clicked on Databases > Attach and clicked Add... > selected the AdventureWorks2012_Data file. and then I Selected the log file and removed the log file by clicking on the Remove button then clicked OK but I still get an error that the header file is not a valid database header file and the FILESIZE property is incorrect ......please help me
As Martin suggested, it may be that the file you downloaded is incorrect. You should download a new copy from here (I wouldn't get it anywhere else):
Once you do, don't use the UI for this. Make sure you copy the .mdf file to your instance's data folder. Then run this code in a query window:
CREATE DATABASE AdventureWorks2012
ON (name = 'AdventureWorks2012_data',
filename = 'drive:\path\AdventureWorks2012_Data.mdf')
FOR ATTACH_REBUILD_LOG;
You will get this "error" message:
File activation failure. The physical file name "drive:\path\AdventureWorks2012_Log.ldf" may be incorrect.
This is just SQL Server telling you that it didn't find the log file; it should still create one for you unless you have other issues (permission denied, lack of space, the same name file already exists, etc).

stata odbc sqlfile

I am trying to load data from database (either MS Access or SQL server) using odbc sqlfile it seems that the code is running with any error but I am not getting data. I am using the following code odbc sqlfile("sqlcode.sql"),dsn("mysqlodbcdata"). Note that sqlcode.sql contains just sql statement with SELECT. The thing is that the same sql code is giving data with odbc load,exec(sqlstmt) dsn("mysqlodbcdata"). Can anyone suggest how can I use odbc sqlfile to import data? This would be a great help for me.
Thanks
Joy
sqlfile doesn't load any data. It just executes (and displays the results when the loud option is specified), without loading any data into Stata. That's somewhat counter-intuitive, but true. The reasons are somewhat opaquely explained in the pdf/dead tree manual entry for the odbc command.
Here's a more helpful answer. Suppose you have your SQL file named sqlcode.sql. You can open it in Stata (as long as it's not too long, where too long depends on your flavor of Stata). Basically, -file read- reads the SQL code line by line, storing the results in a local macro named exec. Then you pass that macro as an argument to the -odbc load- command:
Updated Code To Deal With Some Double Quotes Issues
Cut & paste the following code into a file called loadsql.ado, which you should put in directory where Stata can see it (like ~/ado/personal). You can find such directories with the -adopath- command.
program define loadsql
*! Load the output of an SQL file into Stata, version 1.3 (dvmaster#gmail.com)
version 14.1
syntax using/, DSN(string) [User(string) Password(string) CLEAR NOQuote LOWercase SQLshow ALLSTRing DATESTRing]
#delimit;
tempname mysqlfile exec line;
file open `mysqlfile' using `"`using'"', read text;
file read `mysqlfile' `line';
while r(eof)==0 {;
local `exec' `"``exec'' ``line''"';
file read `mysqlfile' `line';
};
file close `mysqlfile';
odbc load, exec(`"``exec''"') dsn(`"`dsn'"') user(`"`user'"') password(`"`password'"') `clear' `noquote' `lowercase' `sqlshow' `allstring' `datestring';
end;
/* All done! */
The syntax in Stata is
loadsql using "./sqlfile.sql", dsn("mysqlodbcdata")
You can also add all the other odbc load options, such as clear, as well. Obviously, you will need to change the file path and the odbc parameters to reflect your setup. This code should do the same thing as -odbc sqlfile("sqlfile.sql"), dsn("mysqlodbcdata")- plus actually load the data.
I also added the functionality to specify your DB credentials like this:
loadsql using "./sqlfile.sql", dsn("mysqlodbcdata") user("user_name") password("not12345")
For "--XYZ" style comments, do something like this (assuming you don't have "--" in your SQL code):
if strpos(`"``line''"', "--") > 0 {;
local `line' = substr(`"``line''"', 1, strpos(`"``line''"', "--")-1);
};
I had to post this as an answer otherwise the formatting would've been all messed up, but it's obviously referring to Dimitriy's code.
(You could also define a local macro holding the position of the "--" string to make your code a little cleaner.)

Running Powershell scripts through SQL

I have a script that runs Invoke-SQLCmd against a SQLServer called Server1. Data that is collected from that is passed along to another script that is fired off against Server2 and the results are inserted back into a table on Server 1. On every Invoke-SQLCmd I have used the -user -password with an account that has sa permissions on both systems.
When i run the script from the command shell or from the Poershell ISE my data is inserted into the table and every thing works fine; When i run it from within SQL nothing happens. I get no outputs ("null" is returned) when i use xp_cmdshell as below.
xp_cmdshell 'powershell.exe -file c:\script.ps1 -ExecutionPolicy Unrestricted'
I have put it into a SQLjob and used a proxy account that links to my domain account that has admin rights on both boxes yet still no results recorded in the job history and no data in my table on Server1.
What am i doing wrong? surely this should work if it works from the ISE?
I haven't had any problem doing this and even created a couple of blog posts:
http://sev17.com/2009/04/05/executing-powershell-in-sql-server (mirror)
http://sev17.com/2010/11/29/executing-powershell-in-sql-server-redux (mirror)
The one thing that I'm doing differently is using the -command parameter with a file name instead of -file, but that shouldn't make a difference. I'm also enclosing the file name in double quotes but this shouldn't make a difference either if the script file has not spaces in the file path.
Outside of that I would need to see what your script is doing. For instance is it connecting to other machines? Can you run a simple command in powershell like 'powershell -command get-command'?
It would seem that i was not loading the snapins correctly. Although my SQLsnapins were loaded for the first session it wasnt passing it to the second PS script that was running invoke-SQLcmd. My second script did add the cmdlet snapin but that may not have been enough.
It should have worked but for whatever reason, adding the script block from here fixed it. :/
http://msdn.microsoft.com/en-us/library/cc281962.aspx
Thanks to those that responded.
What am i doing wrong?
I think there is an error in the example provided. I would have expected:
xp_cmdshell 'powershell.exe -ExecutionPolicy Unrestricted -file c:\script.ps1'
Because: "File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters and their values."
Source: PowerShell.exe Command-Line Help