Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Friday, March 30, 2012

run-time changeable queries

I need to extract and store a value from a table (or from a MS Access file with OpenDataSource) which is not always the same and it is therefore stored in the @.openfile variable. Something like this:
...
declare @.standardselect nvarchar(4000)
declare @.value int
select @.standardSelect='select top 1 @.value=val from ' + @.openfile
exec (@.standardSelect)
...

It obviously doesn't work because the variable @.value is not declared within the sql string.
However, since @.openfile is always different, I need to pass it through a string and the only way I know is within a variable. If I declare @.value inside the @.standardselect it is not accessible to the rest of the procedure, which is not acceptable for me.

Any suggestions?I have solved it using a temporary table, rather than a variable, where to store the value val. But if you have a better idea...|||

You can use EXEC statement

@.sSQL = 'select...from ' + @.tablename + ' where ...'
Exec @.sSQL

However this is a bad practice.

|||Andranik Khachatryan, I guess you haven't read the code in my first message?

Andranik Khachatryan wrote:

You can use EXEC statement

@.sSQL = 'select...from ' + @.tablename + ' where ...'
Exec @.sSQL

However this is a bad practice.

|||

Oops sorry :)

My bad, I am a bit careless today.

|||

You could use sp_executesql with output parameters to do this.

Declare @.StandardSelect nvarchar(4000)
Declare @.Value Int

Select @.StandardSelect = 'Select top 1 @.Value=Id From ' + @.OpenFile
exec sp_executesql @.StandardSelect, N'@.Value int output', @.value OUTPUT

Select @.value

|||

You don't really need dynamic SQL for doing this. You can do below instead:

declare @.source varchar(30)

-- ... initialize based on whether you want to query table or Access

set @.source =

declare @.value int

set @.value = (

select top 1 val from (

select val from your_table where @.source = 'table'

union all

select val from opendatasource(...) where @.source = 'access'

) as t

order by ...

)

Monday, March 26, 2012

Running Transact-SQL script file from within Visual Studio.net

My VB.net project needs to run a .sql file containing transact-sql codes. Is there a way to do this using a connection within VB.net?

Thanks,

Hi Dots
you can read the file as a text file and pass the string of the file to a SQLCommand object|||You will have to check for BATCH separator in the file which requires parsing the file. It is probably easier to just spawn a process like SQLCMD/OSQL and use it to execute the script. You can capture output, error code from the command-line utility and use it in the program.|||

Hi,

I am trying to do what you suggested but can;t get it to work

This is my code...


System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @."sqlcmd -i ""C:\Program Files\PCIT\CDEvolution\CreateDB.sql"" -o ""C:\Program Files\PCIT\CDEvolution\CreateDBRes.txt""";

Console.WriteLine(proc.StartInfo.FileName.ToString());

try { proc.Start(); }
catch (Exception e)
{
Console.WriteLine(e.Message);
}

but when I run it I get an erro message saying the file can't be found.

I have tried:

Placing .exe after the "sqlcmd"

Using AppDomain.CurrentDomain.BaseDirectory before the string with and without using the full string for the fille names.

The files do exits in the location spicified but I get the same result every time

How do I get this to work?

Cheers.

PS. You may have noticed I am using C#

Running Transact-SQL script file from within Visual Studio.net

My VB.net project needs to run a .sql file containing transact-sql codes. Is there a way to do this using a connection within VB.net?

Thanks,

Hi Dots
you can read the file as a text file and pass the string of the file to a SQLCommand object|||You will have to check for BATCH separator in the file which requires parsing the file. It is probably easier to just spawn a process like SQLCMD/OSQL and use it to execute the script. You can capture output, error code from the command-line utility and use it in the program.|||

Hi,

I am trying to do what you suggested but can;t get it to work

This is my code...


System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @."sqlcmd -i ""C:\Program Files\PCIT\CDEvolution\CreateDB.sql"" -o ""C:\Program Files\PCIT\CDEvolution\CreateDBRes.txt""";

Console.WriteLine(proc.StartInfo.FileName.ToString());

try { proc.Start(); }
catch (Exception e)
{
Console.WriteLine(e.Message);
}

but when I run it I get an erro message saying the file can't be found.

I have tried:

Placing .exe after the "sqlcmd"

Using AppDomain.CurrentDomain.BaseDirectory before the string with and without using the full string for the fille names.

The files do exits in the location spicified but I get the same result every time

How do I get this to work?

Cheers.

PS. You may have noticed I am using C#

Friday, March 23, 2012

Running the Package input file .xmla

Hi,

Is there a way to run the package input .xmla file to the command prompt instead of running it in management studio?

cherriesh

Try the ascmd command-line utility, part of Analysis Services Administrative Samples.

http://www.microsoft.com/downloads/details.aspx?FamilyID=e719ecf7-9f46-4312-af89-6ad8702e4e6e&DisplayLang=en

sql

Wednesday, March 21, 2012

Running SSIS as an Agent job using a proxy

Hello,
I built a simple SSIS package using BIDS.
The package does a backup of a DB to a network share.
I imported the dtsx file into SQL Server using SQL Server WB (connected to
the SSIS server)
I created a SQL Agent job to run the SSIS package.
I have created a credential that maps to a domain account (BackupUser1). The
domain account has access to the network share: I verified it by running a
command line using the Run As and accessing the network share as that user.
Of course, I have a sql agent proxy associated to the credentials which is
assigned to run the job step that executes the SSIS package.
When the job runs it fails because of access denied to the network share.
To see what actually happened, I changed the permissions and the share (and
the NTFS permissions) to allow R/W access to 'everyone'.
I re-scheduled the job and this time it succeeds. I went over to the backup
file that was created when the SSIS package executed and discovered that the
owner (i.e. the creator) of the file is not the domain account (BackupUser1),
rather it's the data base server computer account name DBSRV$
this means that SQL Server Agent didn't impersonate the account defined by
the SQL Agent proxy.
I looked for information and wasn't able to find an answer...
A couple of things I made in the local security policy (user rights
assignment):
allowed the specified domain account the permission to log-on as a batch job
allowed the SYSTEM principal (the agent runs under LocalSystem) to replace
process level token (i.e. impersonate, at least as I understand this user
right)
Any thoughts, suggestion on why SQL Server Agent doesn’t actually use the
credentials associated with the proxy account to access the network share?
Thanks,
Asher
Hello Asher,
As for this issue, I've found your another new thread in the following
newsgroup:
Subject: Running SSIS pakcage as an agent job using a proxy
Date: Thu, 21 Sep 2006 02:58:02 -0700
Newsgroups: microsoft.public.sqlserver.server
I've posted my reply there with some suggestion. Please feel free to
followup there if you have any further finding or questions.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
|||Hello Asher,
As for this issue, I've found your another new thread in the following
newsgroup:
Subject: Running SSIS pakcage as an agent job using a proxy
Date: Thu, 21 Sep 2006 02:58:02 -0700
Newsgroups: microsoft.public.sqlserver.server
I've posted my reply there with some suggestion. Please feel free to
followup there if you have any further finding or questions.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Running SSIS as an Agent job using a proxy

Hello,
I built a simple SSIS package using BIDS.
The package does a backup of a DB to a network share.
I imported the dtsx file into SQL Server using SQL Server WB (connected to
the SSIS server)
I created a SQL Agent job to run the SSIS package.
I have created a credential that maps to a domain account (BackupUser1). The
domain account has access to the network share: I verified it by running a
command line using the Run As and accessing the network share as that user.
Of course, I have a sql agent proxy associated to the credentials which is
assigned to run the job step that executes the SSIS package.
When the job runs it fails because of access denied to the network share.
To see what actually happened, I changed the permissions and the share (and
the NTFS permissions) to allow R/W access to 'everyone'.
I re-scheduled the job and this time it succeeds. I went over to the backup
file that was created when the SSIS package executed and discovered that the
owner (i.e. the creator) of the file is not the domain account (BackupUser1),
rather it's the data base server computer account name DBSRV$
this means that SQL Server Agent didn't impersonate the account defined by
the SQL Agent proxy.
I looked for information and wasn't able to find an answer...
A couple of things I made in the local security policy (user rights
assignment):
allowed the specified domain account the permission to log-on as a batch job
allowed the SYSTEM principal (the agent runs under LocalSystem) to replace
process level token (i.e. impersonate, at least as I understand this user
right)
Any thoughts, suggestion on why SQL Server Agent doesnâ't actually use the
credentials associated with the proxy account to access the network share?
Thanks,
AsherHello Asher,
As for this issue, I've found your another new thread in the following
newsgroup:
Subject: Running SSIS pakcage as an agent job using a proxy
Date: Thu, 21 Sep 2006 02:58:02 -0700
Newsgroups: microsoft.public.sqlserver.server
I've posted my reply there with some suggestion. Please feel free to
followup there if you have any further finding or questions.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Running SSIS as an Agent job using a proxy

Hello,
I built a simple SSIS package using BIDS.
The package does a backup of a DB to a network share.
I imported the dtsx file into SQL Server using SQL Server WB (connected to
the SSIS server)
I created a SQL Agent job to run the SSIS package.
I have created a credential that maps to a domain account (BackupUser1). The
domain account has access to the network share: I verified it by running a
command line using the Run As and accessing the network share as that user.
Of course, I have a sql agent proxy associated to the credentials which is
assigned to run the job step that executes the SSIS package.
When the job runs it fails because of access denied to the network share.
To see what actually happened, I changed the permissions and the share (and
the NTFS permissions) to allow R/W access to 'everyone'.
I re-scheduled the job and this time it succeeds. I went over to the backup
file that was created when the SSIS package executed and discovered that the
owner (i.e. the creator) of the file is not the domain account (BackupUser1),
rather it's the data base server computer account name DBSRV$
this means that SQL Server Agent didn't impersonate the account defined by
the SQL Agent proxy.
I looked for information and wasn't able to find an answer...
A couple of things I made in the local security policy (user rights
assignment):
allowed the specified domain account the permission to log-on as a batch job
allowed the SYSTEM principal (the agent runs under LocalSystem) to replace
process level token (i.e. impersonate, at least as I understand this user
right)
Any thoughts, suggestion on why SQL Server Agent doesnâ't actually use the
credentials associated with the proxy account to access the network share?
Thanks,
AsherHello Asher,
As for this issue, I've found your another new thread in the following
newsgroup:
Subject: Running SSIS pakcage as an agent job using a proxy
Date: Thu, 21 Sep 2006 02:58:02 -0700
Newsgroups: microsoft.public.sqlserver.server
I've posted my reply there with some suggestion. Please feel free to
followup there if you have any further finding or questions.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Running SSIS as an Agent job using a proxy

Hello,
I built a simple SSIS package using BIDS.
The package does a backup of a DB to a network share.
I imported the dtsx file into SQL Server using SQL Server WB (connected to
the SSIS server)
I created a SQL Agent job to run the SSIS package.
I have created a credential that maps to a domain account (BackupUser1). The
domain account has access to the network share: I verified it by running a
command line using the Run As and accessing the network share as that user.
Of course, I have a sql agent proxy associated to the credentials which is
assigned to run the job step that executes the SSIS package.
When the job runs it fails because of access denied to the network share.
To see what actually happened, I changed the permissions and the share (and
the NTFS permissions) to allow R/W access to 'everyone'.
I re-scheduled the job and this time it succeeds. I went over to the backup
file that was created when the SSIS package executed and discovered that the
owner (i.e. the creator) of the file is not the domain account (BackupUser1)
,
rather it's the data base server computer account name DBSRV$
this means that SQL Server Agent didn't impersonate the account defined by
the SQL Agent proxy.
I looked for information and wasn't able to find an answer...
A couple of things I made in the local security policy (user rights
assignment):
allowed the specified domain account the permission to log-on as a batch job
allowed the SYSTEM principal (the agent runs under LocalSystem) to replace
process level token (i.e. impersonate, at least as I understand this user
right)
Any thoughts, suggestion on why SQL Server Agent doesn’t actually use the
credentials associated with the proxy account to access the network share?
Thanks,
AsherHello Asher,
As for this issue, I've found your another new thread in the following
newsgroup:
Subject: Running SSIS pakcage as an agent job using a proxy
Date: Thu, 21 Sep 2006 02:58:02 -0700
Newsgroups: microsoft.public.sqlserver.server
I've posted my reply there with some suggestion. Please feel free to
followup there if you have any further finding or questions.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Tuesday, March 20, 2012

running sql server data marked suspect

hi all,
I run application & database on same machine and suddenly my database marked
suspect and data base is unavailable.
I copy mdf file to other location and try to attached data but it show me
error number 3624 any one help my I don't know why the database marked
suspect and how can I resolve this problem if any one has solution of this
problem then please help me as soon as possible
Thanks in advance
Regards
khurramhttp://www.karaszi.com/SQLServer/in..._suspect_db.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Syed khurram alam" <khurram.alam@.eintelligencesoft.com> wrote in message
news:e1X$u1gVFHA.2684@.TK2MSFTNGP09.phx.gbl...
> hi all,
> I run application & database on same machine and suddenly my database mark
ed suspect and data base
> is unavailable.
> I copy mdf file to other location and try to attached data but it show me
error number 3624 any
> one help my I don't know why the database marked suspect and how can I res
olve this problem if any
> one has solution of this problem then please help me as soon as possible
> Thanks in advance
> Regards
> khurram
>

running sql server data marked suspect

hi all,
I run application & database on same machine and suddenly my database marked
suspect and data base is unavailable.
I copy mdf file to other location and try to attached data but it show me
error number 3624 any one help my I don't know why the database marked
suspect and how can I resolve this problem if any one has solution of this
problem then please help me as soon as possible
Thanks in advance
Regards
khurram
http://www.karaszi.com/SQLServer/inf...suspect_db.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Syed khurram alam" <khurram.alam@.eintelligencesoft.com> wrote in message
news:e1X$u1gVFHA.2684@.TK2MSFTNGP09.phx.gbl...
> hi all,
> I run application & database on same machine and suddenly my database marked suspect and data base
> is unavailable.
> I copy mdf file to other location and try to attached data but it show me error number 3624 any
> one help my I don't know why the database marked suspect and how can I resolve this problem if any
> one has solution of this problem then please help me as soon as possible
> Thanks in advance
> Regards
> khurram
>

running sql server data marked suspect

hi all,
I run application & database on same machine and suddenly my database marked
suspect and data base is unavailable.
I copy mdf file to other location and try to attached data but it show me
error number 3624 any one help my I don't know why the database marked
suspect and how can I resolve this problem if any one has solution of this
problem then please help me as soon as possible
Thanks in advance
Regards
khurramhttp://www.karaszi.com/SQLServer/info_corrupt_suspect_db.asp
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Syed khurram alam" <khurram.alam@.eintelligencesoft.com> wrote in message
news:e1X$u1gVFHA.2684@.TK2MSFTNGP09.phx.gbl...
> hi all,
> I run application & database on same machine and suddenly my database marked suspect and data base
> is unavailable.
> I copy mdf file to other location and try to attached data but it show me error number 3624 any
> one help my I don't know why the database marked suspect and how can I resolve this problem if any
> one has solution of this problem then please help me as soon as possible
> Thanks in advance
> Regards
> khurram
>

Tuesday, February 21, 2012

running SQL file (view + procedure) problem


I am try to run a .sql file
CREATE VIEW LastReport
AS
SELECT MAX(ID) AS LastReport, RHost
FROM Report
WHERE (RComplete = 1)
GROUP BY RHost
CREATE PROCEDURE [dbo].[MembresAcces_Insert]
AS
INSERT INTO membresAcces (login, passe, id_membresTypes) VALUES ('admin','admin',50);

... 10 view and procedure like this one
i get an error view must be the first one
what is wrong ? running CREATE TABLE works perfectly
thank you


Try placing a GO in between each statement.
CREATE VIEW LastReport
AS
SELECT MAX(ID) AS LastReport, RHost
FROM Report
WHERE (RComplete = 1)
GROUP BY RHost
GO
CREATE PROCEDURE [dbo].[MembresAcces_Insert]
AS
INSERT INTO membresAcces (login, passe, id_membresTypes) VALUES ('admin','admin',50);
GO

running sql file

hi does sql server has any utility to run sql file from OS
level...Hi,
You can use the OSQL or ISQL utilities from command prompt to execute a SQL
file from command prompt.
Sample:
OSQL -Sserver_name -Usa -Ppassword -ic:\test.sql -oc:\output.log
(From command prompt execute OSQL/? to get all the options available)
Thanks
Hari
MCDBA
<anonymous@.discussions.microsoft.com> wrote in message
news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
> hi does sql server has any utility to run sql file from OS
> level...|||If you have the sql client utilities installed then look into using osql
command line utility for the same.
HTH,
Vinod Kumar
MCSE, DBA, MCAD, MCSD
http://www.extremeexperts.com
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp
<anonymous@.discussions.microsoft.com> wrote in message
news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
> hi does sql server has any utility to run sql file from OS
> level...|||Thanks..Is it possible to run a script from stored
procedure?
>--Original Message--
>Hi,
>You can use the OSQL or ISQL utilities from command
prompt to execute a SQL
>file from command prompt.
>Sample:
>OSQL -Sserver_name -Usa -Ppassword -ic:\test.sql -
oc:\output.log
>(From command prompt execute OSQL/? to get all the
options available)
>Thanks
>Hari
>MCDBA
><anonymous@.discussions.microsoft.com> wrote in message
>news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
OS[vbcol=seagreen]
>
>.
>|||Hi,
Yes, it is possible to do using xp_cmdshell.
Sample:-
alter proc test_proc
as
begin
exec master..xp_cmdshell
'osql -Usa -Ppassword -Sserver_name -ic:\hari.sql -oc:\hari.log'
end
Thanks
Hari
MCDBA
<anonymous@.discussions.microsoft.com> wrote in message
news:f99701c43e39$eed68270$a001280a@.phx.gbl...[vbcol=seagreen]
> Thanks..Is it possible to run a script from stored
> procedure?
> prompt to execute a SQL
> oc:\output.log
> options available)
> OS

running sql file

hi does sql server has any utility to run sql file from OS
level...
Hi,
You can use the OSQL or ISQL utilities from command prompt to execute a SQL
file from command prompt.
Sample:
OSQL -Sserver_name -Usa -Ppassword -ic:\test.sql -oc:\output.log
(From command prompt execute OSQL/? to get all the options available)
Thanks
Hari
MCDBA
<anonymous@.discussions.microsoft.com> wrote in message
news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
> hi does sql server has any utility to run sql file from OS
> level...
|||If you have the sql client utilities installed then look into using osql
command line utility for the same.
HTH,
Vinod Kumar
MCSE, DBA, MCAD, MCSD
http://www.extremeexperts.com
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
<anonymous@.discussions.microsoft.com> wrote in message
news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
> hi does sql server has any utility to run sql file from OS
> level...
|||Thanks..Is it possible to run a script from stored
procedure?
>--Original Message--
>Hi,
>You can use the OSQL or ISQL utilities from command
prompt to execute a SQL
>file from command prompt.
>Sample:
>OSQL -Sserver_name -Usa -Ppassword -ic:\test.sql -
oc:\output.log
>(From command prompt execute OSQL/? to get all the
options available)[vbcol=seagreen]
>Thanks
>Hari
>MCDBA
><anonymous@.discussions.microsoft.com> wrote in message
>news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
OS
>
>.
>
|||Hi,
Yes, it is possible to do using xp_cmdshell.
Sample:-
alter proc test_proc
as
begin
exec master..xp_cmdshell
'osql -Usa -Ppassword -Sserver_name -ic:\hari.sql -oc:\hari.log'
end
Thanks
Hari
MCDBA
<anonymous@.discussions.microsoft.com> wrote in message
news:f99701c43e39$eed68270$a001280a@.phx.gbl...[vbcol=seagreen]
> Thanks..Is it possible to run a script from stored
> procedure?
> prompt to execute a SQL
> oc:\output.log
> options available)
> OS

running sql file

hi does sql server has any utility to run sql file from OS
level...Hi,
You can use the OSQL or ISQL utilities from command prompt to execute a SQL
file from command prompt.
Sample:
OSQL -Sserver_name -Usa -Ppassword -ic:\test.sql -oc:\output.log
(From command prompt execute OSQL/? to get all the options available)
Thanks
Hari
MCDBA
<anonymous@.discussions.microsoft.com> wrote in message
news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
> hi does sql server has any utility to run sql file from OS
> level...|||If you have the sql client utilities installed then look into using osql
command line utility for the same.
--
HTH,
Vinod Kumar
MCSE, DBA, MCAD, MCSD
http://www.extremeexperts.com
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
<anonymous@.discussions.microsoft.com> wrote in message
news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
> hi does sql server has any utility to run sql file from OS
> level...|||Thanks..Is it possible to run a script from stored
procedure?
>--Original Message--
>Hi,
>You can use the OSQL or ISQL utilities from command
prompt to execute a SQL
>file from command prompt.
>Sample:
>OSQL -Sserver_name -Usa -Ppassword -ic:\test.sql -
oc:\output.log
>(From command prompt execute OSQL/? to get all the
options available)
>Thanks
>Hari
>MCDBA
><anonymous@.discussions.microsoft.com> wrote in message
>news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
>> hi does sql server has any utility to run sql file from
OS
>> level...
>
>.
>|||Hi,
Yes, it is possible to do using xp_cmdshell.
Sample:-
alter proc test_proc
as
begin
exec master..xp_cmdshell
'osql -Usa -Ppassword -Sserver_name -ic:\hari.sql -oc:\hari.log'
end
Thanks
Hari
MCDBA
<anonymous@.discussions.microsoft.com> wrote in message
news:f99701c43e39$eed68270$a001280a@.phx.gbl...
> Thanks..Is it possible to run a script from stored
> procedure?
> >--Original Message--
> >Hi,
> >
> >You can use the OSQL or ISQL utilities from command
> prompt to execute a SQL
> >file from command prompt.
> >
> >Sample:
> >
> >OSQL -Sserver_name -Usa -Ppassword -ic:\test.sql -
> oc:\output.log
> >
> >(From command prompt execute OSQL/? to get all the
> options available)
> >
> >Thanks
> >Hari
> >MCDBA
> >
> ><anonymous@.discussions.microsoft.com> wrote in message
> >news:f96d01c43e32$21fafbc0$a001280a@.phx.gbl...
> >> hi does sql server has any utility to run sql file from
> OS
> >> level...
> >
> >
> >.
> >